locate
$ locate *.txt # 在 index 中搜索文件$ locate -i some*.txt # 忽略大小写$ sudo updatedb # 更新 index 文件
find
find [starting-point]... [option]... [operator]... [test]... [action]...
- If no starting-point is specified,
.is assumed.Tests
| Test | Description | | —- | —- | | -name pattern | Match files and directories with the specified wildcard pattern. | | -type c | Match files of type c. See File Types. | | -size [+|-]n | Match files of size n. See Size Units | | -perm [-|/]mode | Match files or directories that have permissions set to the specified mode. mode can be expressed by either octal or symbolic notation.
- mode 精确匹配 mode 指定的权限
-find . -perm 644
-find . -perm u=rw,g=r,o=r
-find . -perm ugo=rw
- -mode 至少包括 mode 指定的权限
-find . -perm -220
-find . -perm -g+w,u+w
- /mode 满足 mode 指定的权限中的一个即可
-find . -perm /220
-find . -perm /u+w,g+w
-find . -perm /u=w,g=w
| | -cmin n | Match files or directories whose content or attributes were last modified exactly n minutes ago. To specify less than n minutes ago, use -n, and to specify more than n minutes ago, use +n. | | -cnewer file | Match files or directories whose contents or attributes were last modified more recently than those of file. | | -ctime n | Match files or directories whose contents or attributes were last modified n24 hours ago. | | -empty | Match empty files and directories. | | -group name | Match file or directories belonging to belonging to group name. name may be expressed may be expressed either as a group name or as a numeric group ID. | | -iname pattern | Like the -name test but case-insensitive. | | -inum n | Match files with inode number n. This is helpful for finding all the hard links to a particular inode. | | -mmin n | Match files or directories whose contents were last modified n minutes ago. | | -mtime n | Match files or directories whose contents were last modified n24 hours ago. | | -newer file | Match files and directories whose contents were modified more recently than the specified file. This is useful when writing shell scripts that perform file backups. Each time you make a backup, update a file (such as a log) and then use find to determine which files have changed since the last update. | | -nouser | Match file and directories that do not belong to a valid user. This can be used to find files belonging to deleted accounts or to detect activity by attackers. | | -nogroup | Match files and directories that do not belong to a valid group. | | -samefile name | Similar to the -inum test. Match files that share the same inode number as file name. | | -user name | Match files or directories belonging to user name. The user may be expressed by a username or by a numeric user ID. |
File Types
| File type | Description |
|---|---|
| b | Block special device file |
| c | Character special device file |
| d | Directory |
| f | Regular file |
| l | Symbolic link |
Size Units
| Character | Unit |
|---|---|
| b | 512-byte blocks. This is the default if no unit is specified. |
| c | Bytes. |
| w | 2-byte words |
| k | Kilobytes (units of 1,024 bytes). |
| M | Megabytes (units of 1,048,576 bytes). |
| G | Gigabytes (units of 1,073,741,824 bytes). |
Operators
| Operator | Description |
|---|---|
| -and | Match if the tests on both sides of the operator are true. This can be shortened to -a. Note that when no operator is present, -and is implied by default.find -type f -a -size +1M |
| -or | Match if a test on either side of the operator is true. This can be shortened to -o.find -type f -o -type -d |
| -not | Match if the test following the operator is false. This can be abbreviated with an exclamation point (!).find -type f \\! -size +1M |
| ( ) | Group tests and operators together to form larger expressions. This is used to control the precedence of the logical evaluations. By default, find evaluates from left to right. It is often necessary to override the default evaluation order to obtain the desired result. Even if not needed, it is helpful sometimes to include the grouping characters to improve the readability of the command. Note that since the parentheses have special meaning to the shell, they must be quoted when using them on the command line to allow them to be passed as arguments to find. Usually the backslash character is used to escape them.find ~ \\( -type f -not -perms 0600 \\) -or \\( -type d -not -perms 0700 \\) |
Actions
Predefined Actions
| Action | Description |
|---|---|
| -delete | Delete the currently matching file. |
| -ls | Perform the equivalent of ls -dils on the matching file. Output is sent to standard output. |
| Output the full pathname of the matching file to standard output. This is the default action if no other action is specified. | |
| -print0 | Same ass -print, but produces null-separated output. |
| -quit | Quit once a match has been made. |
User-Defined Actions
find ... -exec COMMAND \{\} \;find ... -exec COMMAND '{}' ';'
- COMMAND is the name of a command,
- {} is a symbolic representation of the current pathname,
- The semicolon is a required delimiter indicating the end of the command.
find ... -ok COMMAND '{}' ';'
- Execute a user-defined action interactively.
By using the -ok action in place of -exec, the user is prompted before execution of each specified command.
Combine Results with
+find ~ -type f -name 'foo*' -exec ls -l '{}' +By changing the trailing semicolon character to a plus sign, we activate the capability of find to combine the results of the search into an argument list for a single execution of the desired command.
Combine Results with
xargsfind ~ -type f -name 'foo*' -print | xargs ls -lxargsaccepts input from standard input and converts it into an argument list for a specified command.--nullor-0: Accept null-separated input(filenames containing embedded spaces)Options
| Option | Description | | —- | —- | | -depth | Direct find to process a directory’s files before the directory itself. This option is automatically applied when the -delete action is specified. | | -maxdepth levels | Set the maximum number of levels that find will descend into a directory tree when performing tests and actions. | | -mindepth levels | Set the minimum number of levels that find will descend into a directory tree before applying tests and actions. | | -mount | Direct find not to traverse directories that are mounted on other file systems. | | -noleaf | Direct find not to optimize its search based on the assumption that it is searching a Unix-like file system. This is needed when scanning DOS/Windows file systems and CD-ROMs. |
