find
- Finds files in the specified directory
added
find
Command to search for files in the specified directory.- Any string before the parameter is treated as the name of the directory you are looking for.
- If you use this command without setting any parameters, the find command will find subdirectories and files in the current directory.
- All subdirectories and files found are displayed.
grammar
Find (option)(parameter)Copy the code
options
-amin< min > : Finds files or directories that have been accessed at a specified time, in minutes; -anewer< reference file or directory > : finds a file or directory whose access time is closer to the current one than the specified file or directory. -atime<24 hours > : searches for files or directories that have been accessed at a specified time, expressed in 24 hours. -cmin< minute > : searches for files or directories that have been changed at the specified time. -cnewer< reference files or directories > finds newer files or directories whose change time is closer to that of the specified file or directory; -ctime<24 hours > : searches for files or directories that have been changed at a specified time, in 24 hours. -daystart: calculates the time from today. -depth: Searches for the deepest subdirectory in a specified directory. -expty: searches for files with 0 Byte file size or empty directories that do not have any subdirectories or files. -exec< execute instruction > : execute the find instruction if the return value is True; -false: sets the return value of the find command to false. -fls< list file > : This parameter has the same effect as the "-ls" parameter, but saves the result as the specified list file. -follow: exclude symbolic joins; -fprint< list file > : This parameter has the same effect as the "-print" parameter, but saves the result to the specified list file. -fprint0< list file > : This parameter has the same effect as "-print0" parameter, but saves the result to the specified list file. -fprintf< list file >< Output format > : This parameter has the same effect as the "-printf" parameter, but saves the result to the specified list file; -fstype< file system type > : only files or directories under this file system type are searched. -gid< group id > : searches for files or directories that match the specified group id. -group< group name > : searches for files or directories that match the specified group name. -help or -- help: online help; -ilname< Template style > : This parameter has the same effect as the "-lname" parameter, but ignores the difference in case. -iname< Template style > : The effect of this parameter is similar to that of the "-name" parameter, but the difference in case is ignored. -inum<inode number > : searches for the file or directory that meets the specified inode number. -ipath< Template style > : The effect of this parameter is similar to that of the "-path" parameter, but the difference in case is ignored. -iregex< Template Style > : The effect of this parameter is similar to that of the -regexe parameter, but the difference in case of characters is ignored. -links< number of connections > : finds files or directories that match the specified number of hard connections; -iname< template style > : specifies the string as the template style for finding symbolic links; -ls: Lists file or directory names to standard output, assuming that the return value of the find command is true. -maxdepth< directory depth > : sets the maximum directory depth. -mindepth< directory level > : sets the minimum directory level. -mmin< minute > : searches for files or directories that have been changed at a specified time, in minutes. -mount: The effect of this parameter is the same as that of -xdev. -mtime<24 hours > : searches for files or directories that have been changed within a specified period of time, in 24 hours. -name< template style > : specifies a string as the template style to find files or directories; -newer< reference files or directories > : finds files or directories whose change time is closer to the current one than the specified file or directory. -nogroup: finds files or directories that do not belong to the local host group identifier. - Noleaf: does not consider that directories need to have at least two hard connections; -nouser: finds files or directories that do not belong to the local host user id. -ok< execute command > : The effect of this parameter is similar to that of -exec, but the user is asked before executing the command. If y or y is answered, the command is abandoned. -path< template style > : specifies a string as the template style for finding directories. -perm< permission value > : searches for files or directories that match the specified permission value. -print: lists the file or directory name to standard output, assuming the return value of the find command is true. The format is one name for each column, and each name is preceded by a./ string. -print0: lists the file or directory name to standard output, assuming the return value of the find command is true. All names in the format are on the same line; -printf< output format > : Lists file or directory names to standard output, assuming the return value of the find directive is true. The format can be customized; -prune: does not find strings as a template style for finding files or directories; -regex< template style > : specifies a string as the template style for finding files or directories. -size< file size > : searches for files that match the specified file size. -true: Sets the return value of the find command to true. -type< file type > : searches for files that match the specified file type. -uid< user id > : searches for files or directories that match the specified user id. -used< days > : searches for files or directories that were accessed at a specified time after the file or directory was changed, in days. -user< owner name > : finder and file or directory with the specified owner name. -version or -- version: displays the version information. -xdev: limits the range to the first file system. -xtype< file type > : This parameter has the same effect as the "-type" parameter except that it checks for symbolic links.Copy the code
parameter
Start directory: Start directory for searching files.
The instance
#Search for all files in the current directory. The file content contains 140.206.111.111The find. -type f - the name '*' | xargs grep "140.206.111.111"Copy the code
Matches based on files or regular expressions
Lists all files and folders in the current directory and subdirectories
find .
Copy the code
Look for the file names ending in. TXT in the /home directory
find /home -name "*.txt"
Copy the code
Same as above, but case is ignored
find /home -iname "*.txt"
Copy the code
Find all files ending in. TXT and. PDF in the current directory and subdirectories
Find. \ (-name "*.txt" - o -name "*. PDF \") or find -name "*.txt - o - the name" PDF "" *.Copy the code
Matches the file path or file
find /usr/ -path "*local*"
Copy the code
Matches file paths based on regular expressions
find . -regex ".*\(\.txt\|\.pdf\)$"
Copy the code
Same as above, but case is ignored
find . -iregex ".*\(\.txt\|\.pdf\)$"
Copy the code
No parameters
Find files under /home that do not end in.txt
find /home ! -name "*.txt"
Copy the code
Search by file type
Find. -type Indicates the type parameterCopy the code
Type parameter list:
- F Common files
- L sign connection
- D directory
- C character device
- B block device
- S a socket
- p Fifo
Deep search based on directory
The maximum downward depth limit is 3
find . -maxdepth 3 -type f
Copy the code
Search for all files that are at least 2 subdirectories away from the current directory
find . -mindepth 2 -type f
Copy the code
Search by file timestamp
Find. -type f TimestampCopy the code
UNIX/Linux file systems have three timestamps for each file:
- Access time (-atime/ day, -amin/ min) : indicates the last access time.
- Modification time (-mtime/ day, -mmin/ minute) : indicates the time when a file is last modified.
- Change time (-ctime/ day, -cmin/ minute) : indicates the time when file data elements (such as permissions) are modified for the last time.
Search for all files accessed in the last seven days
find . -type f -atime -7
Copy the code
Search for all files that were accessed exactly seven days ago
find . -type f -atime 7
Copy the code
Search for all files accessed more than seven days ago
find . -type f -atime +7
Copy the code
Search for all files that have been accessed for more than 10 minutes
find . -type f -amin +10
Copy the code
Find all files that took longer to modify than file.log
find . -type f -newer file.log
Copy the code
Match by file size
Find. -type f-size Specifies the file size unitCopy the code
File size unit:
- B — Block (512 bytes)
- C – byte
- W — word (2 bytes)
- K — kilobytes
- M — megabytes
- G — gigabytes
Search for files larger than 10KB
find . -type f -size +10k
Copy the code
Search for files smaller than 10KB
find . -type f -size -10k
Copy the code
Search for files equal to 10KB
find . -type f -size 10k
Copy the code
Deleting matching files
Delete all. TXT files in the current directory
find . -type f -name "*.txt" -delete
Copy the code
Matches by file permission/ownership
Search for files with permission 777 in the current directory
find . -type f -perm 777
Copy the code
Find PHP files in the current directory that are not 644 privileges
find . -type f -name "*.php" ! -perm 644
Copy the code
Find all files owned by the current directory user Tom
find . -type f -user tom
Copy the code
Find all files owned by the current directory user group Sunk
find . -type f -group sunk
Copy the code
With the help of-exec
Option is used in combination with other commands
Find all root files in the current directory and change ownership to user Tom
find .-type f -user root -exec chown tom {} \;
Copy the code
In the example above, {} is used in conjunction with the -exec option to match all files, which are then replaced with the corresponding file names.
Find all the.txt files in your home directory and delete them
find $HOME/. -name "*.txt" -ok rm {} \;
Copy the code
In the above example, -ok behaves the same as -exec, except that it gives a hint as to whether to perform the corresponding operation.
Find all the.txt files in the current directory and splice them into the all.txt file
find . -type f -name "*.txt" -exec cat {} \; > /all.txtCopy the code
Move the.log file from 30 days ago to the old directory
find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
Copy the code
Find all the. TXT files in the current directory and print them as “File: filename”
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
Copy the code
Because multiple commands cannot be used in the -exec parameter of a single command, the following method can be used to accept multiple commands after -exec
-exec ./text.sh {} \;
Copy the code
Searches but jumps out of the specified directory
Find all. TXT files in the current directory or subdirectory, but skip subdirectory sk
find . -path "./sk" -prune -o -name "*.txt" -print
Copy the code
Find other skills
List all files with length zero
find . -empty
Copy the code
Other instances
Find ~ -name '* JPG '# find all JPG files in home directory. The -name argument allows you to limit the results to files that match a given pattern. Find ~ -iname '* JPG '# -iname is like -name, but case insensitive find ~ (-iname 'jpeg' -o-iname' JPG ') # Some images may have.jpeg extensions. Fortunately, we can combine patterns with "or" (represented as -o). Find ~ \(-iname '*jpeg' -o-iname '* JPG '\) -type f # What if you have some directories that end in JPG? (Why you want to name a bucketofJPG instead of pictures directory is beyond the scope of this article.) We use the -type parameter to modify our command to find the file. Find ~ \(-iname '*jpeg' -o-iname '* JPG '\) -type d # Maybe you want to find those oddly named directories so you can rename them laterCopy the code
A lot of photos have been taken recently, so let’s zoom it down to the file that was changed last week
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7
Copy the code
You can perform time filtering based on file state change time (Ctime), change time (mtime), or access time (atime). These are over a few days, so if you want more fine-grained control, you can express them in minutes (Cmin, Mmin, and Amin, respectively). Unless you know exactly what time you want, you might add a number after + (greater than) or – (less than).
But maybe you don’t care about your photos. Maybe you’re running low on disk space, so you want to find all the huge (let’s define “greater than 1GB”) files in the log directory:
find /var/log -size +1G
Copy the code
Or, maybe you want to find all the files bcotton owns in /data:
find /data -owner bcotton
Copy the code
You can also look up files by permission. Maybe you want to find files in your home directory that are readable to everyone to make sure you don’t overshare.
find ~ -perm -o=r
Copy the code
Delete files automatically generated on the MAC
find ./ -name '__MACOSX' -depth -exec rm -rf {} \;
Copy the code
Count lines of code
. Find -name "*. Java" | xargs cat | grep -v ^ $| wc - l # lines of code, ruled out a blank lineCopy the code