
find是Linux/Unix系统中最强大的文件查找工具,能够在指定目录树中根据文件名、类型、大小、时间、权限等多种条件进行搜索。与locate这类基于数据库的搜索工具不同,find直接扫描文件系统,虽然速度较慢,但能保证结果的实时性和准确性,同时支持对找到的文件执行后续操作。
find的功能远不止于查找文件,它内置了强大的表达式组合能力和丰富的操作选项,是系统管理、日志清理、批量处理、安全审计等场景的核心工具。掌握find是提升Linux运维效率的关键技能。
1. 基本搜索
| 命令 | 说明 |
|---|
find . -name "file.txt" | |
find . -iname "readme.md" | |
find /etc -name "*.conf" | |
find . -type d -name "backup" | |
find . -name "*.txt" -o -name "*.log" | |
2. 按类型过滤
| 命令 | 说明 |
|---|
find . -type f | |
find . -type d | |
find . -type l | |
find . -type f -name "*.log" | |
find . -maxdepth 1 -type f | |
find . -type f -empty | |
find . -type d -empty | |
find . -type f -perm -u+x | |
3. 按大小过滤
| 命令 | 说明 |
|---|
find . -type f -size +100M | |
find . -type f -size -10M | |
find . -type f -size 1G | |
find /var -type f -size +500M | |
find . -type f -size +100M -size -1G | |
find . -type f -size +1k | |
find . -type f -size -1M -exec ls -lh {} \; | |
4. 按时间过滤
| 命令 | 说明 |
|---|
find . -type f -mtime -7 | |
find . -type f -mtime +30 | |
find . -type f -mtime 7 | |
find . -type f -atime -1 | |
find . -type f -ctime -3 | |
find . -type f -mmin -60 | |
find . -type f -newer reference.txt | |
find . -type f ! -newer reference.txt | |
时间单位说明:
5. 按权限和所有权过滤
| 命令 | 说明 |
|---|
find . -type f -perm 644 | |
find . -type f -perm -u+w | |
find . -type f -perm -o=r | |
find . -type f -perm -2000 | |
find / -type f -user root | |
find /srv -type f -group www-data | |
find . -type f -user 1000 | |
find . -type f ! -user root | |
6. 排除路径
| 命令 | 说明 |
|---|
find . -path ./node_modules -prune -o -type f -print | |
find . \( -path ./node_modules -o -path ./.git \) -prune -o -type f -print | |
find . -type f ! -name "*.log" | |
find . -type f ! -path "*/cache/*" | |
find . -type f -name "*.txt" -not -path "*/backup/*" | |
find . -type f -name "*.log" ! -name "error*" | |
7. 执行操作(-exec, -delete)
| 命令 | 说明 |
|---|
find . -type f -name "*.tmp" -delete | |
find . -type f -name "*.log" -exec gzip {} \; | |
find . -type f -name "*.jpg" -exec mv {} /tmp/images/ \; | |
find . -type f -name "*.conf" -exec grep -H "listen" {} \; | |
find . -type f -name "*.log" -exec rm {} + | |
find . -type f -name "*.sh" -exec chmod +x {} \; | |
find . -type f -name "*.md" -exec wc -l {} \; | |
find . -type f -name "*.jpg" -exec ls -lh {} + | |
{} 与 ;/+ 说明:
8. 安全批量操作(空字符分隔)
使用-print0配合xargs -0处理包含空格、换行符的特殊文件名:
| 命令 | 说明 |
|---|
find . -type f -name "*.txt" -print0 | xargs -0 rm -f | |
find . -type f -print0 | xargs -0 ls -lh | |
find . -type f -name "*.log" -print0 | xargs -0 du -h | |
find . -type f -name "*.bak" -print0 | xargs -0 -I{} mv "{}" /tmp/backup/ | |
find . -type f -name "*.pdf" -print0 | xargs -0 chmod 644 | |
9. 实用组合模式
| 命令 | 说明 |
|---|
find /var/log -type f -name "*.log" -mtime +90 -delete | |
find . -type f -size +1G -exec ls -lh {} \; | |
find . -type d -empty -delete | |
find . -type f -perm 777 -ls | |
find / -type f -user $(whoami) 2>/dev/null | |
find . -type f -name "*.py" -exec grep -l "TODO" {} \; | |
find . -type f -printf "%s %p\n" | sort -rn | head -10 | |
find . -type f -name "*.jpg" -exec identify {} \; | |
10. 常用选项速查
| 选项 | 说明 |
|---|
-name | |
-iname | |
-type | |
-size | 按大小过滤(c=字节,k=KB,M=MB,G=GB) |
-mtime | |
-mmin | |
-atime | |
-ctime | |
-perm | |
-user | |
-group | |
-maxdepth | |
-mindepth | |
-prune | |
-exec | |
-delete | |
-print0 | |
-ls | |
11. 故障排查
| 问题 | 解决方法 |
|---|
| find: paths must precede expression | 通配符需要引号:find . -name "*.txt" 而非 find . -name *.txt |
| 权限拒绝错误过多 | |
| 查找速度太慢 | 使用-maxdepth限制深度;考虑使用locate替代 |
| -exec命令出错 | |
| 文件名包含空格处理错误 | |
| 删除前需要确认 | 先用-ls预览,或用-ok替代-exec(逐条确认) |
| 查找不到某些文件 | |
| 排除目录不生效 | -prune需与其他条件正确组合,-prune -o模式是常用写法 |
温馨提示: find命令非常强大,但也可能造成不可逆的后果(如批量删除)。建议遵循以下原则:1)批量操作前先用-ls或-printf预览结果;2)使用-exec前先测试命令;3)删除操作优先使用-delete而非-exec rm;4)处理特殊文件名时使用-print0和xargs -0;5)使用-maxdepth限制搜索范围提升性能。在脚本中使用find时,建议使用变量引用路径,避免路径中含有空格导致问题。
关注公众号(haopython),请回复: LLTFIND