服务器上文件找不到?find 太慢、locate 找不到新文件、which 和 whereis 分不清?本文对比 4 个常用查找命令,帮你选对工具、用对场景。
一、四款工具速览
二、find — 功能最强,但最慢
1. 基础用法
# 按文件名查找(当前目录递归)find . -name "nginx.conf"# 忽略大小写find . -iname "nginx.conf"# 按类型查找find . -type f -name "*.log" # 普通文件find . -type d -name "logs" # 目录find . -type l -name "link" # 符号链接# 按大小查找find /var/log -size +100M # 大于100MBfind /var/log -size -1k # 小于1KBfind /var/log -size 10M # 刚好10MB# 按时间查找find /var/log -mtime -7 # 7天内修改过find /var/log -mtime +30 # 30天前修改过find /var/log -atime +90 # 90天前访问过find /var/log -ctime +1 # 1天前状态变更过# 按权限查找find / -perm 644 # 精确权限644find / -perm /u=s # 查找 SUID 文件
2. 执行操作
# 查找并删除find /tmp -type f -name "*.tmp" -delete# 查找并执行命令find /var/log -name "*.log" -exec ls -lh {} \; # 每个文件执行一次find /var/log -name "*.log" -exec ls -lh {} + # 批量执行(效率高)# 查找并复制find /data/app -name "*.conf" -exec cp {} /backup/ \;# 查找并修改权限find /data/app -type f -exec chmod 644 {} \;find /data/app -type d -exec chmod 755 {} \;
3. 组合条件
# 与关系(默认)find /var/log -name "*.log" -size +100M# 或关系find /var/log \( -name "*.log" -o -name "*.gz" \)# 非关系find /var/log ! -name "*.gz"# 复杂组合find /data -type f \( -name "*.tmp" -o -name "*.bak" \) -mtime +7 -delete
4. 高级用法
# 查找空文件/目录find /var/log -empty# 查找大于1G的文件,按大小排序find / -type f -size +1G -exec ls -lhS {} + | head -20# 查找并统计数量find /var/log -name "*.log" | wc -l# 限制目录深度find /var/log -maxdepth 2 -name "*.log"# 排除特定目录find / -path /proc -prune -o -name "*.conf" -print# 查找硬链接find /data -samefile /data/file.txt# 查找 inodefind / -inum 1234567
5. 性能优化
# 指定文件系统类型,避免跨挂载点find / -xdev -name "*.conf"# 先用 -name 过滤,减少 -exec 调用find /var/log -name "*.log" -size +100M -exec rm {} \;# 大量文件时用 xargs 替代 -execfind /var/log -name "*.log" | xargs rm# 或更安全的find /var/log -name "*.log" -print0 | xargs -0 rm
三、locate — 极速模糊查找
1. 原理
locate 基于预构建的数据库索引查找,不遍历磁盘,所以极快。
# 安装sudo yum install mlocate # CentOS/RHEL/Rockysudo apt install plocate # Ubuntu/Debian(新版更快)# 更新数据库(每天自动更新,手动更新用)sudo updatedb# 查找locate nginx.conf
2. 特点
3. 常用用法
# 基础查找(包含路径)locate nginx.conf# 只显示文件名(不含路径)locate -b '\nginx.conf'# 显示匹配数量locate -c nginx# 限制输出数量locate -n 10 nginx.conf# 使用正则表达式locate -r '/etc/.*\.conf$'# 忽略大小写locate -i nginx
4. 配置
# 数据库配置/etc/updatedb.conf# 常见配置项PRUNEFS="9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs configfs debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"PRUNENAMES=".git .hg .svn"PRUNEPATHS="/afs /media /mnt /net /sfs /tmp /udev /var/cache /var/lib/pacman/local /var/lib/yum /var/spool /var/tmp"# 排除不索引的目录,减少数据库大小
四、which — 找命令位置
1. 用途
查找 PATH 环境变量 中可执行命令的绝对路径。
which python/usr/bin/pythonwhich nginx/usr/sbin/nginxwhich java/usr/bin/java# 找不到时返回空which mycommand# 无输出
2. 特点
3. 扩展用法
# 显示所有匹配路径which -a python/usr/local/bin/python/usr/bin/python# 显示别名which -a lsalias ls='ls --color=auto' /usr/bin/ls
五、whereis — 找命令、源码、手册
1. 用途
在标准路径中查找命令的二进制文件、源码、手册页。
whereis nginxnginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gzwhereis pythonpython: /usr/bin/python /usr/bin/python3.11 /usr/lib/python3.11 /etc/python /usr/share/man/man1/python.1.gz
2. 选项
# 只找二进制whereis -b nginxnginx: /usr/sbin/nginx /usr/lib64/nginx# 只找手册whereis -m nginxnginx: /usr/share/man/man8/nginx.8.gz
3. 与 which 对比
六、选型指南
| | |
|---|
| | which python |
| | whereis nginx |
| | locate nginx.conf |
| | find / -name "*.conf" -mtime -7 |
| | find / -size +1G |
| | find /tmp -empty |
| | find / -perm /u=s |
| | find . -name "*.log" -delete |
| | find . -mtime -1 |
| | find . -perm 644 |
七、生产实战
场景1:清理日志
# 找30天前的日志并删除find /var/log/app -name "*.log" -mtime +30 -delete# 或压缩find /var/log/app -name "*.log" -mtime +7 -exec gzip {} \;
场景2:找配置文件
# 快速定位 nginx 配置locate nginx.conf# 精确查找并排除备份find /etc/nginx -name "*.conf" ! -name "*.conf.bak"
场景3:找大文件释放空间
# 找大于100MB的文件find / -xdev -type f -size +100M -exec ls -lhS {} + | head -20# 找最近7天增长最快的目录find /var/log -type d -mtime -7 -exec du -sh {} \; | sort -rh | head -10
场景4:安全审计
# 找所有 SUID 文件(潜在提权风险)find / -perm /u=s -type f 2>/dev/null# 找所有777权限的文件find /data -type f -perm 777# 找没有属主的文件find / -nouser -o -nogroup 2>/dev/null
场景5:批量修改
# 批量替换配置文件中的 IPfind /etc/nginx -name "*.conf" -exec sed -i 's/192.168.1.100/10.0.0.50/g' {} \;# 批量修改属主find /data/app -user olduser -exec chown newuser:newuser {} \;
八、总结 checklist
□ which:找命令位置,看 PATH,脚本判断命令是否存在□ whereis:找命令+手册+源码,标准路径搜索□ locate:快速模糊查找,记得 updatedb,新文件可能找不到□ find:功能最全,实时、递归、条件组合、执行操作□ find 性能:-xdev 不跨文件系统,-maxdepth 限制深度□ find -exec vs xargs:大量文件用 xargs 效率更高□ locate 数据库:/etc/updatedb.conf 配置排除目录
一句话总结
which 问 PATH,whereis 问系统,locate 问数据库,find 问磁盘——四个工具四个场景,find 最慢但万能,locate 最快但有延迟。