find command
find command used to find files or directories, according to the file or directory required for a given path and find expression.
This article only summarizes some common usage, a more detailed description see the man find and info the Find .
Syntax
find [参数] [路径] [查找和搜索范围]
- The default is the current path, if high server load, try not to use the find command during peak periods
- The default parameter is -print
Common parameters
parameter | Explanation |
---|---|
-name | Find by name |
-size | Find by size |
-user | Find by file owner |
-type | Search by type |
-mtime | Find the file in accordance with the change of time |
-atime | Find in accordance with the access time of the file |
-perm | Find by file permissions |
-regex | According to the regular expression to find |
In most cases, -name enough to solve many problems, encounter complex situations, you can use regular -regex.
Note that -regex not match the file name, but matching the full file name (including the path) .
Simple examples
Use -name parameter check the / etc directory ending in .conf all of the following profile:
find /etc -name '*.conf'
Use -size parameter check the / etc directory files larger than 2M:
find /etc -size +2M
Search all files in the current directory to be accessed within the next seven days:
find . -type f -atime -7
Identify under / var / log does not end with .log file:
find /var/log/ -type f ! -name '*.log'
Find opt / down / logs contain ab2019-05 and ending with .log file:
find /opt/logs/ -regex '.*ab.*2019-05.*.log'
With exec Usage
format
find [参数] [路径] [查找和搜索范围] -exec [命令] \;
Behind with -exec parameter is the command command, its termination based on ";" as the end of the flags, so the phrase semicolon after the command is indispensable, taking into account the various systems semicolon have different meanings, so preceded by a backslash.
Examples
Check current attributes of all files in the directory:
find . -type f -exec ls -l { } \;
Find / opt / logs Log files older than 7 days and delete them:
find /opt/logs -name '*.log' -mtime +7 -exec rm -rf {} \;
Note that in a production environment as much as possible not to use rm -rf.