linux command - find
find
search for files in a directory hierarchy
1 | find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression] |
-H, -L, -P
选项 -H
, -L
, -P
用来控制软连接的处理。
- -H: 默认行为,不搜索软连接下的目录或文件。
- -L: 搜索软链接。
- -P: 不搜索软链接下的目录或文件,除非
当没有指定 -H
, -L
, -P
时,-H
是默认行为,即不搜索软链接下的目录和文件。同时指定多个选项,最后一个有效。如:find -H -L .
,-H
无效,-L
有效。
-D debugopts, -Olevel
- -D debugopts: 打印诊断信息
- -Olevel: 允许查询优化,
level
: 0, 1, 2, 3
starting-point
查询开始路径,默认为当前目录 .
。
expression
expression 表达式可以由以下部分组成:tests, actions, global options, positional options, operators。
tests
-name pattern
按目录名或文件名查找。pattern
不能够有/
,否则无效,如:-name a/b
无效。1
2
3find -name "apps.py" # 查找当前目录下的 apps.py 文件
find -name "apps" # 查找当前目录下名称为 apps 的文件或目录
find /home -name "app*" # 查找 /home 目录下名称匹配 app* 的文件或目录-iname pattern
iname(insensitive name). 用法同-name
,只是pattern
对大小写不敏感。-path pattern
按路径查找。pattern
是路径。1
2
3
4find -path "apps" # 无结果,因为 apps 不是路径
find -path "./apps" # 查找当前目录下的 apps 目录或文件
find -path "./apps*" # 查找当前目录下路径以 apps 开头的目录或文件
find /home -path "*apps*" # 在 /home 目录下查找包含 apps 的目录或文件-ipath pattern
insensitive path, 用法同-path
, 只是pattern
对大小写不敏感。-regex pattern
和-path
差不多。-user uname
按照文件或目录所属用户查找。-group gname
按照文件或目录所属组查找。
更多选项查看 find --help
或 man find
。
actions
-exec command ;
对搜索的结果执行命令command
。如果命令是用搜索结果作为参数的,可以用{}
作为占位符。1
2find -path "./apps*" -exec
find -path "./apps*" -exec file {} \;-ok command ;
类似-exec
,不过在执行命令command
之前先要经过用户同意 (y/n)
operators
(expr)
优先操作符,和一般运算的()
一样。! expr
取反expr1 expr2
与,即expr1
和expr2
,相当于 Python 表达式expr1 and expr2
。expr1 -a expr2
同expr1 expr2
expr1 -and expr2
同expr1 expr2
,但是不遵循 POSIX。expr1 -o expr2
或,即expr1
或expr2
,相当于 Python 表达式expr1 or expr2
。expr1 -or expr2
同expr1 -o expr2
,但是不遵循 POSIX。