Common usage of find command
Linux下,常见的文件搜索命令,
locate
find
ag
locate
用法简单,根据数据库查找,非实时,用法:
locate FILENAME
手动更新数据库(时间可能较长)
updatedb
find
实时,精确,功能强大,用法:
find 路径 查找标准 查找后动作
(1)路径:. 或者 ./ 均表示当前目录
(2)查找标准:
-name 'FILENAME' 文件名精确匹配(支持通配符* ? [])
-iname 'FILENAME' 文件名模糊匹配(不分大小写)
-regex PATTERN 正则匹配
-user USERNAME 根据属主查找
-group GROUP 根据属组查找
-uid UID
-gid GID
-nouser 无属主文件
-type 根据文件类型查找
-type f 文件
-type d 目录
-type c 字符设备
-type b 块设备
-type l 链接
-type p 管道
-type s 套接字
-size 根据文件大小查找
-size 10k
-size 25M
-size 3G
-size +10k 大于10k的文件
-size -5M 小于5M的文件
组合选项:
-a AND
-o OR
-not NOT
根据时间查找
-mtime modified time(默认单位:天)
-ctime change time
-atime access time
-ctime +5 改变时间5天以上
-access -3 访问时间3天以内
-mmin (默认单位:分钟)
-cmin
-amin
根据权限查找
-perm 755 精确权限
-perm /644 三位中有一位匹配到即可
-perm -700 向下包含(包含600/500/.../000)
例1:查找/tmp目录下无属主的普通文件
find /tmp -nouser -a -type d
例2:查找/etc下,既不是普通文件也不是目录的其他文件
find /etc -not \( -type d -o -type f \)
(3)查找后动作
-print 默认动作
-ls 列表
-ok COMMAND \; 需确认执行COMMAND
-exec COMMAND \; 无需确认执行COMMAND
例3:查找当前目录下权限为600的所有文件,并查看其大小({}表示查找到的文件,\;表示结束符)
find . -perm 600 -exec du {} \;
例4:查找当前目录下权限是400的文件,并将其后缀名加上.new
find ./ -perm 400 -exec mv {} {}.new \;
Disclaimer
- License under
CC BY-NC 4.0
- Copyright issue feedback
me#imzye.me
, replace # with @ - Not all the commands and scripts are tested in production environment, use at your own risk
- No privacy information is collected here