Find Command
1.1. Find command Options
-name Search by file name
-perm Follow file permissions to find
-prune can use the Find command to exclude the current folder and not find
-user can be found according to the owner of the file
-group can be found in the file array.
-mtime-n +n Follow the file change time to find
-n means that the file change distance is within n days +n means that the file change time distance is now n days ago.
-nogroup finding files for groups that are not valid
-nouser finding files that are not valid for the master
-type Finding a file of a certain type
B represents the device block
Catalog D
C-Character device files
l Symbolic Link file
F Common Files
-size Find file length or size
-depth Find a file, first look for the current file, the file in the directory, and then look in its subdirectories
-mount file system that does not cross mount when looking up the file system
-follow If the find command encounters a symbolic link file, the source file to which the link file points is traced
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(1), using the-name option
Find Files
Find/test–name "*.txt" –print
Find/test–name "[0-9].txt" –print
(2), using the PERM option
Follow permissions to find
Find/test-perm 755–print
(3), ignore a directory
Ignore/TEST/AA Directory
#cd/test
Alvin.txt
find/opt/-path "/opt/rh/test/aa"-prune-o-name "4.txt"-print
(4), using the user and NOUSESR options
# Find/test-user Mysql–print
/test/1.txt
Find files that have been deleted from the master account
# Groupadd Alvin
# Useradd Alvin–g Alvin
# chown Alvin:alvin 1.txt
#userdel –r Alvin
# Find/test-nouser-print
/test/1.txt
(5), using Group and Nogroup options
# Find/test-group Mysql–print
/test/1.txt
Find files that have been deleted by the main group
# Groupadd Alvin
# Useradd Alvin–g Alvin
# chown Alvin:alvin 1.txt
#userdel –r Alvin
# Find/test–nogroup-print
/test/1.txt
(6). Find Files by change time
Locate the system root directory and change the files within 5 days.
# Find/-mtime-5–print
Locate the system root directory, and change the file to 3 days before the time.
# Find/–mtime +3–print
(7), find newer or older files than a file
Query a new file than 1.txt
# Find-newer 1.txt–print
Query than 1.txt new but older than alvin.txt files
Find. -newer 1.txt! -newer Alvin.txt-print |xargs Ls–l
(8), using Type option-type b/d/c/p/l/f #查是块设备, directory, character device, pipe, symbolic link, normal file
Find all the directories below/test
# Find/test-type D–print
Find all the L connection files under/etc
Find/etc-type L-print | Xargs Ls-l | More
(9), use the size option
Find files larger than 10M in the current catalog file
# Find. -size +10m-print | Xargs DU–LH
Find files that are less than 10M in the current catalog file
# Find. -size-10m-print | Xargs DU–LH
------------|xargs: When the transferred value is empty, execute the following command directly.
(10), using the depth option
Go to sub-directory lookup
# find/test-depth-name "Zy.txt" –print
(11), using the Mount option
-mount means unable to collapse the file system lookup, its current Mount file system lookup
# Find/-mount-name "Alvinzeng.txt"-print
(13), using EXEC or OK to execute shell commands
Find. -type f-exec ls-l {} \; Do not prompt
Find. -type f–ok ls-l {} \; Safety Tips
-ok after the general root Delete command RML
Find/test-type f–ok rm {} \;
1.2. Xargs command
Xargs is more convenient than exec
Find/test–name "* *" –print | Xargs ls–l
Xargs
Xargs can handle stdin and convert it to command-line arguments for specific commands, Xargs can also convert single-line or multiline text input to other formats.
Convert multi-line output to single-line output
[email protected] sepultra]# Cat A
1
23
456
7890
[email protected] sepultra]# cat A|xargs
1 23 456 7890
Convert a single line of input into multiple lines of input
[email protected] sepultra]# Cat A
0 1 2 3 4 5 6 7 8 9
[email protected] sepultra]# Cat A|xargs-n 3
0 1 2
3 4 5
6 7 8
9
-d Specifies a delimiter (by default, internal character separator ') #echo "Splitxsplitxsplitxsplit" | xargs-d x
-n Specifies the maximum number of parameters per row
Read stdin, pass formatting parameters to command
Cat Args.txt | Xargs-n 2./cecho.sh
Cat Args.txt | Xargs-i {}./cecho.sh-p {}-1
#-i{} Specifies the replacement character layer. For each command argument, the string is replaced with the parameter read from stdin, and when I is used, the command executes in a loop, and if there are 3 parameters, the command is executed 3 times with {}, and the {} is replaced with the corresponding parameter in each execution.
Find+xargs
Find. -type f-name "*.txt"-print0 | xargs-0 rm-rf# Delete all txt files
Find Source_code_dir_path-type f-name "*.c"-print0|xargs-0 wc-l# Statistics the number of lines in the source code directory for all C program Files
The difference between Xargs and exec in the Find command
When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching objects to the command behind exec to execute. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.
The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on.
In some systems, the use of the-EXEC option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there will be too many processes and degraded system performance, so the efficiency is not high;
With the Xargs command, there is only one process. In addition, when using the Xargs command, whether to get all the parameters at once or to get the parameters in batches, and the number of parameters to get each time will be determined according to the command's options and the corresponding tunable parameters in the system kernel.
Find Command--xargs--exec