The find command is used to find files in the specified directory. Any string preceding the parameter will be treated as the name of the directory to be searched. If you use this command without setting any parameters, the find command will search for subdirectories and files in the current directory. And all found subdirectories and files will be displayed.
Syntax
find(option)(parameter)
Options
-amin
-anewer
-atime<24 hours>: Find files that have been accessed at the specified time or Directory, the unit is calculated in 24 hours;
-cmin
-cnewer
-ctime<24 hours>: Find files or directories that were changed at the specified time, the unit is calculated in 24 hours;
-daystart: Starting from today Calculation time;
-depth: Start searching from the deepest subdirectory under the specified directory;
-expty: Find files with a file size of 0 Byte, or an empty directory without any subdirectories or files in the directory;
- exec
-false: Set all the return values of the find command to False;
-fls: This The effect of the parameter is similar to that of specifying the "-ls" parameter, but the result will be saved as the specified list file;
-follow: Exclude symbolic links;
-fprint: The effect of this parameter is the same as that of specifying "- print" parameter is similar, but the result will be saved into the specified list file;
-fprint0: The effect of this parameter is similar to specifying the "-print0" parameter, but the result will be saved into the specified list file;
-fprintf
-fstype
-gid
-group
-help or --help: online help; -ilname: The effect of this parameter is similar to the specified "-lname" parameter, but the case of characters is ignored Difference;
-iname: The effect of this parameter is similar to that of specifying the "-name" parameter, but the difference in character case is ignored;
-inum: Find files or directories that match the specified inode number;
-ipath: The effect of this parameter is similar to that of specifying the "-path" parameter, but the difference in character case is ignored;
-iregex: The effect of this parameter is similar to that of specifying "-regexe" The parameters are similar, but the difference in character case is ignored;
-links
-iname: Specify a string as a symbolic link to find Template style;
-ls: Assuming that the return value of the find command is True, list the file or directory names to the standard output;
-maxdepth
-mmin
-mount: The effect of this parameter and specify "-xdev "Same;
-mtime<24 hours>: Find files or directories that have been changed at the specified time, the unit is calculated in 24 hours;
-name: Specify a string as the search file or The template style of the directory;
-newer
-nogroup: Find files or directories that do not belong to the local host group The file or directory of the identification code;
-noleaf: The directory must have at least two hard connections regardless of the existence of it;
-nouser: Find the files or directories that do not belong to the local host user identification code;
-ok
-path : Specify a string as a template pattern for searching directories;
-perm
-print: Assuming that the return value of the find command is True, list the file or directory name to the standard output. The format is one name for each column, and there is a "./" string before each name;
-print0: Assuming that the return value of the find command is True, list the file or directory names to the standard output. The format is that all names are on the same line;
-printf
-prune: Do not search for a string as a template style for finding files or directories;
-regex: Specify a string as a template style for searching for files or directories;
-size< File size>: Find files that match the specified file size;
-true: Set the return values of the find command to True;
-typ
-uid
-used
-user
-version or --version: display version information;
-xdev: Limit the scope to the first file system;
-xtype
Parameters
Start directory: The starting directory to find files.
Example
Match based on files or regular expressions
List all files and folders in the current directory and subdirectories
find .
Find file names ending with .txt in the /home directory
find /home -name "*.txt"
Same as above, but ignore case
find /home -iname "*.txt"
Find all files ending with .txt and .pdf in the current directory and subdirectories
find . ( -name "*.txt" -o -name "*.pdf" )
or
find . -name "*.txt" -o -name "*.pdf"
match file path Or file
find /usr/ -path "*local*"
match file path based on regular expression
find . -regex ".*(.txt|.pdf)$"
Same as above, but ignore case
find . -iregex ".*(.txt|.pdf)$"
Negative parameter
Find files in /home that do not end with .txt
find /home ! -name "*.txt"
Search by file type
find . -type type parameter
type parameter list:
f ordinary file
l symbolic link
d directory
c character device
b block device
s socket
p Fifo Based on directory depth search
Based on directory depth search
The maximum downward depth limit is 3
find . -maxdepth 3 -type f
Search out all files with a depth of at least 2 subdirectories from the current directory
find . -mindepth 2 -type f
Search based on file timestamp
find . -type f timestamp
UNIX/Linux file system Each file has three timestamps:
Access time (-atime/day , -amin/minute):
The user’s last access time.
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 that have been accessed in the last seven days
find . -type f -atime -7
Search all files that were accessed exactly seven days ago
find . -type f -atime 7
Search more than All files that have been accessed within seven days
find . -type f -atime +7
Search for all files that have been accessed for more than 10 minutes
find . -type f -amin +10
Find out more modifications than file.log All files longer
find . -type f -newer file.log
Match based on file size
find . -type f -size
File size unit File size unit:
b —— block ( 512 bytes)
c —— Byte
w —— Word (2 bytes)
k —— Kilobyte
M —— Megabyte
G —— Gigabyte
Search for greater than 10KB files
find . -type f -size +10k
Search for files less than 10KB
find . -type f -size -10k
Search for files equal to 10KB
find . -type f -size 10k
Delete matching files
Delete all .txt files in the current directory
find . -type f -name "*.txt" -delete
Match based on file permissions/ownership
Search for files with permissions of 777 in the current directory Documents
find . -type f -perm 777
Find the php files in the current directory with permissions other than 644
find . -type f -name "*.php" ! -perm 644
Find out the php files owned by the current directory user tom All files
find . -type f -user tom
Find all files owned by user group sunk in the current directory
find . -type f -group sunk
Use the -exec option in conjunction with other commands to find out the current directory Download all root files and change ownership to user tom
find .-type f -user root
-exec chown tom {};
In the above example, {} is used in combination with the -exec option to match All files will then be replaced with the corresponding file names.
Find all the .txt files in your home directory and delete them
find $HOME/.
-name "*.txt"
-ok rm {} ;
In the above example, -ok and -exec The behavior is the same, but it will prompt whether to perform the corresponding operation.
Find all .txt files in the current directory and concatenate them into the all.txt file
find . -type f -name "*.txt" -exec cat {} ;> all.txt
Move the .log files 30 days ago to the old directory
find . -type f -mtime +30 -name "*.log" -exec cp {} old ;
Find all .txt files in the current directory and Print it out in the form of "File: file name"
find . -type f -name "*.txt" -exec printf "File: %sn" {} ;
Because multiple -exec parameters cannot be used in a single-line command command, the following method can accept multiple commands after
-exec
-exec ./text.sh {} ;
Search but jump out of the specified directory
Find all .txt files in the current directory or subdirectory , but skip subdirectoriessk find .
-path "./sk" -prune -o -name "*.txt" -print find
Other tips collection
To list all files with zero length
find . -empty