The use of find and xargs
1. Parameter describes the directory path found by the pathname: find command. For example, with. To represent the current directory and / to represent the system root directory. The-print: find command outputs matching files to standard output. The-exec: find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form 'command' {}\;, notice the space between {} and\;. -ok: the function is the same as-exec, except that the shell command given by this parameter is executed in a more secure mode, and before each command is executed, a prompt is given to the user to determine whether to execute it. #-print outputs the found files to the standard output #-exec command {}\;-- command the found files, {} and\ There are spaces between #-ok and-exec, except that before operation, ask the user-name filename # to find the file named filename-perm # to find the file by execution permission-user username # to find by file owner-group groupname # to find by group-mtime-n + n # to find the file by file change time -n refers to within n days, + n refers to n days ago-atime-n + n # looks up files by file access time: GIN: 0px ">-ctime-n + n # finds files by file creation time,-n refers to within n days, + n refers to n days ago-nogroup # checks files that do not belong to a valid group That is, the subordinate group of the file does not exist in / etc/groups-nouser # check the file with no valid owner, that is, the owner of the file does not save-newer F1! f2 in / etc/passwd to find the file,-n refers to within n days, + n refers to n days ago-ctime-n + n # to find the file according to the time when the file was created, and-n refers to within n days + n refers to n days ago-nogroup # check files without valid group, that is, file group does not exist in / etc/groups-nouser # check files with no valid owner That is, the owner of the file does not exist in / etc/passwd-newer F1! f2 # check for files whose change time is newer than F1 but older than f2-type b/d/c/p/l/f # check for block devices, directories, character devices, pipes, symbolic links, Ordinary files-size n [c] # look for files of n blocks [or n bytes] in length-depth # enables the search to complete the directory before entering the subdirectory-fstype # to find files whose change time is newer than F1 but older than f2-type b/d/c/p/l/f # check is block devices, directories, character devices, pipes, Symbolic links, normal files-size n [c] # look for files of n blocks [or n bytes] in length-depth # causes the search to find the finished directory before entering the subdirectory-fstype # to look for files in a certain type of file system These file system types can usually be found in / etc/fstab-mount # look up files without crossing the file system mount point-follow # if you encounter a symbolic link file, track the file that the link refers to-cpio% # look for files located in a certain type of file system, these file system types can usually be found in / etc/fstab-mount # look for files without crossing the file system mount point-follow # if you encounter a symbolic link file, track the file that the link refers to-cpio # use the cpio command for matching files Backup them to a tape device-prune # ignore a directory-amin # find files accessed in the last N minutes of the system-mmin # find files modified in the last N minutes of the system 2. Basic use
Find /-size + 10m | xargs ls-l-h
# find files larger than 10m in the system and send them to the program behind xargs for processing through the pipeline
Find. -size-1k
# find the current directory, files less than 1k, and note that the directory is also counted.
Find. -type f-size-2k
# find out the current directory, files less than 2K, just files
Find. -name ""-type f-size 0c
# find the empty file in the current directory. 0c means 0 bytes.
Find. -type f-size-10k-exec cp {} / root/233\
# find out the files whose current directory is less than 10K, and run the cp command through exec to copy them to the / root/233 directory. Note that the exec format is weird and ends with\;
Find. -name ".txt" | xargs rm # find the current directory and delete files of type txt.
Find / home-links + 2 # check files or directories with more than 2 hard connections
Find / home-perm 0700 # check files or directories with permissions of 700
Find /-nouser # finds files that belong to invalid users in the system
Find /-user fred # finds files that belong to the user FRED in the system
Find /-empty # looks for files or folders that are empty in the system
Find /-groupcat # finds files that belong to groupcat in the system
Find /-type f-amin-10 # finds the files accessed in the last 10 minutes of the system
Find /-type f-atime-2 # finds the files accessed in the last 48 hours of the system
Find /-type f-empty # looks for files or folders that are empty in the system
Find /-type f-mmin-5 # finds files that have been modified in the last 5 minutes of the system
Find /-type f-mtime-1 # finds files that have been modified in the last 24 hours of the system
Find / home-type f-mtime-2 # check the files that have been changed in the last two days under / home
Find / home-type f-atime-1 # check files accessed within 1 day under / home
Find / home-type f-mmin + 60 # check files changed 60 minutes ago under / home
Find / home-type f-amin + 30 # check files accessed in the last 30 minutes under / home
Find. -type f-atime-7-print # lists all files accessed in the current directory in the last 7 days
Find.-type f-atime 7-print # lists all the files that were accessed in the current directory just before the seventh day
Find. -type f-atime + 7-print # lists all files that have been accessed for more than seven days in the current directory
Find. -type f-newer file.txt-print # find all the files in the current directory that take longer to modify than file.txt, using-newer, which can refer to
(- atime is based on access time;-mtime is based on modification time;-ctime is based on change time. The units of all three parameters are days)
(- amin is based on access time;-mmin is based on modification time;-cmin is based on change time. These three parameters are in minutes)
Advanced usage
Find. -type f-name "* .sh" | xargs wc-l
# find the current directory .sh file and display the number of lines in each file
Echo "11 22 33 14 44 55 V" | xargs-d:-N3
# xargs sort, with: as delimiter, three per line,-d: no default space division