find command in Linux
The find command is used to find files in a specified directory. Any string preceding the parameter will be treated as the directory name to look for.
If you use this command without setting any parameters, the find command will look for subdirectories and files in the current directory. And all found subdirectories and files are displayed.
find syntax
find (选项) (参数)
Commonly used option parameters
-name <范本样式>
: Specify a string as a template style for finding files or directories-regex<范本样式>
: Specify a string as a template style for finding files or directories-size<文件大小>
: Find files matching the specified file size-typ<文件类型>
: Find only files that match the specified file type-path<范本样式>
: Specify a string as a template style for finding directories-perm<权限数值>
: Find files or directories that match the specified permission value-help或——help
: Online help-false
: Set the return value of the find command to False-true
: Set the return value of the find command to True-amin<分钟>
: Find files or directories that have been accessed at a specified time, in minutes-cmin<分钟>
: Find files or directories that have been changed at the specified timeatime<24小时数>
: Find the files or directories that have been accessed at the specified time, the unit is calculated in 24 hoursctime<24小时数>
: Find files or directories that have been changed at the specified time, in 24-hour units-gid<群组识别码>
: Find files or directories that match the specified group ID-group<群组名称>
: Find files or directories that match the specified group nameuid<用户识别码>
: Find files or directories that match the specified user IDuser<拥有者名称>
: The file or directory with the finder and the specified owner name
There are also some parameters ignored as enumeration:
Specific reference: Linux command collection - find command
example
Match against files or regular expressions
- List all files and folders in the current directory and subdirectories
find .
/home
Look for.txt
filenames ending in a directory
find /home -name "*.txt"
Same as above, ignoring case, use iname
.
find /home -iname "*.txt"
- Find all files ending with
.txt
and in the current directory and subdirectories.cfg
find . -name "*.txt" -o -name "*cfg"
- match file path or file
find /usr/ -path "*local*"
- Match file paths based on regular expressions
find . -regex ".*\(\.txt\|\.pdf\)$"
- Case-ignoring regular matching
find . -iregex ".*\(\.txt\|\.pdf\)$"
Negative parameter
find /home ! -name "*.txt"
Find by file type
find . -type 类型参数
Type parameter list:
f
: normal filel
: symbolic linkd
:contentc
: character deviceb
: block devices
: socketp
:Fifo
eg:
find . -type f
find . -type d
Search by file timestamp
find . -type f 时间戳
There are three timestamps for each file in the Linux file system
Access time (-atime/day, -amin/minute): The last access time of the user.
Modification time (-mtime/day, -mmin/minute): The last modification time of the file.
Change time (-ctime/day, -cmin/minute): The last modification time of file data elements (such as permissions, etc.).
Search all files accessed in the last 7 days
find . -type -atime -7
Search all files that were accessed exactly 7 days ago
find . -type -atime 7
Search all files accessed over seven days
find . -type -atime +7
Search all files with access time longer than 10 minutes
find . -type f -amin +10
Match by file size
find . -type f -size 文件大小单元
b
:block (512 bytes)c
:bytew
: word (2 bytes)k
: kilobytesM
: megabytesG
: Gbytes
Search for files larger than 10KB
find . -type f -size +10k
Search for files smaller than 10KB
find . -type f -size -10k
Search for files equal to 10KB
find . -type f -szie 10k
delete matching files
find . -type f -name "*.txt" -delete
Match based on file permissions
Search for files with permissions 777 in the current directory
find . -type f -perm 777
Find out the txt file whose permissions are not 700 in the current directory
find . -type f -name "*.txt" ! -perm 700
Find all files owned by user zhang in the current directory
find . -type f -user zhang
Find out all files owned by the current directory user group gzhang
find . -type f -group gzhang
Find files with length 0
find . -type f -empty