find #
Usage #
bash
find [starting-point...] [expression]
Options #
text
-help
-P
Never follow symbolic links. This is the default behaviour.
-L
Follow symbolic links.
-H
Do not follow symbolic links, except while processing the command line arguments.
-depth
Process each directory's contents before the directory itself.
The -delete action also implies -depth.
-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below the starting-points.
-mindepth levels
Do not apply any tests or actions at levels less than levels (a non-negative integer).
Tests #
text
-type c
File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link
s socket
-name pattern
Base of file name matches shell pattern pattern.
-iname pattern
Like -name, but the match is case insensitive.
-lname pattern
File is a symbolic link whose contents match shell pattern pattern.
-path pattern
File name matches shell pattern pattern.
The metacharacters do not treat `/' or `.' specially.
-ipath pattern
Like -path. but the match is case insensitive.
-regex pattern
File name matches regular expression pattern.
-iregex pattern
Like -regex, but the match is case insensitive.
-gid n
File's numeric group ID is n.
-uid n
File's numeric user ID is n.
-group gname
File belongs to group gname (numeric group ID allowed).
-user uname
File is owned by user uname (numeric user ID allowed).
-writable
Matches files which are writable.
-readable
Matches files which are readable.
-executable
Matches files which are executable and directories which are searchable.
-perm mode
File's permission bits are exactly mode (octal or symbolic).
-perm -mode
All of the permission bits mode are set for the file.
-perm /mode
Any of the permission bits mode are set for the file.
-size n[cwbkMG]
File uses n units of space, rounding up.
The + and - prefixes signify greater than and less than, as usual.
The following suffixes can be used:
b for 512-byte blocks (this is the default if no suffix is used)
c for bytes
w for two-byte words
k for Kibibytes (KiB, units of 1024 bytes)
M for Mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes)
G for Gibibytes (GiB, units of 1024 * 1024 * 1024 = 1073741824 bytes)
-amin n
File was last accessed n minutes ago.
-atime n
File was last accessed n*24 hours ago.
-mmin n
File's data was last modified n minutes ago.
-mtime n
File's data was last modified n*24 hours ago.
Actions #
text
-ls
List current file in ls -dils format on standard output.
-print
Print the full file name on the standard output, followed by a newline.
-print0
Print the full file name on the standard output, followed by a null character.
-fprint file
Print the full file name into file file, followed by a newline.
-fprint0 file
Print the full file name into file file, followed by a null character.
-delete
Delete files; true if removal succeeded.
If the removal failed, an error message is issued.
If -delete fails, find's exit status will be nonzero (when it eventually exits).
-exec command \;
-exec command \{\} \;
Like -exec, but the command line is built by appending each selected file name at the end.
-exec command \{\} +
The command line is built by appending each selected file name at the end.
-execdir command \;
-execdir command \{\} \;
Like -exec, but the specified command is run from the subdirectory containing the matched file.
-execdir command \{\} +
The command line is built by appending each selected file name at the end.
-quit
Exit immediately.
For example, find /tmp/foo /tmp/bar -print -quit will print only /tmp/foo.
Operators #
text
\( expr \)
\! expr
-not expr
expr1 expr2
expr1 -a expr2
expr1 -and expr2
expr1 -o expr2
expr1 -or expr2
expr1 , expr2
Both expr1 and expr2 are always evaluated.
Examples #
Print file path #
bash
find ./apple/ -type f -print
Change dir for each item #
bash
# Print parent dir path of each item
find ./apple/ -execdir pwd \;
Compress files #
bash
# Compress .js/.css files
find ./apple/ \( -name '*.js' -o -name '*.css' \) -exec bash -c 'gzip -c "{}" > "{}.gz"' \;
追踪软连接 #
bash
# 使用 -L 参数来追踪软连接,而不是当做一个文件来处理。这个参数必须放在查询位置参数前面。
find -L ./apple/ -name '*'
错误示例 #
bash
# 错误写法:-exec指令后面跟的命令不能用引号引起来,要用空格正确分割
find ./apple/ -type d -exec 'ls -l ;'
# 正确写法:
find ./apple/ -type d -exec ls -l \;