find 常用查找与批量操作速查

常用命令

find . -name '*.log'
find . -iname '*.jpg'
find /var -type f -size +100M
find . -type d -name node_modules
find . -mtime -7
find . -mmin -30
find . -user nginx
find . -perm 644
find . -maxdepth 2 -type f
find . -type f -print0 | xargs -0 wc -l
find . -type f -exec chmod 644 {} +
find . -name '*.tmp' -delete
find . -type f -newer reference.txt

条件速查

条件 含义
-name / -iname 名称/忽略大小写
-type f/d/l 文件/目录/链接
-size +10M 大于 10M
-mtime -7 7 天内修改
-mmin -30 30 分钟内修改
-user / -group 属主/属组
-perm 权限
-maxdepth 最大深度
-not / ! 取反
-print0 NUL 分隔,安全处理空格

场景速查

目的 写法
排除目录 find . -path ./node_modules -prune -o -type f -print
批量删除 find . -name '*.tmp' -delete
批量执行 find . -type f -exec grep -l pattern {} +
按时间清理 find /tmp -type f -mtime +7 -delete
统计大小 `find . -type f -printf '%s %p\n'

易错点

相关命令