当前位置:首页>Linux>Linux 文件查找:find、locate、which、whereis 用法对比

Linux 文件查找:find、locate、which、whereis 用法对比

  • 2026-09-08 22:58:45
Linux 文件查找:find、locate、which、whereis 用法对比
服务器上文件找不到?find 太慢、locate 找不到新文件、which 和 whereis 分不清?本文对比 4 个常用查找命令,帮你选对工具、用对场景。

一、四款工具速览

工具
查找范围
速度
实时性
适用场景
find
指定目录递归
慢(遍历磁盘)
✅ 实时
复杂条件、精确查找
locate
数据库索引
极快
❌ 非实时(每天更新)
快速模糊查找
which
PATH 环境变量
极快
✅ 实时
找可执行命令位置
whereis
标准路径
快
✅ 实时
找命令、源码、手册

二、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. 特点

特点
说明
只找可执行文件
普通文件、目录找不到
只在 PATH 中找
不在 PATH 中的命令找不到
找第一个匹配
多个同名命令只返回第一个

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. 选项

选项
说明
-b
只找二进制文件
-m
只找手册页
-s
只找源码
-B
指定搜索二进制文件的路径
-M
指定搜索手册页的路径
# 只找二进制whereis -b nginxnginx: /usr/sbin/nginx /usr/lib64/nginx# 只找手册whereis -m nginxnginx: /usr/share/man/man8/nginx.8.gz

3. 与 which 对比

场景
用 which
用 whereis
找命令位置
✅
✅
找命令手册
❌
✅
找命令源码
❌
✅
找 PATH 外命令
❌
✅(标准路径更广)
脚本中判断命令是否存在
✅(返回码)
❌

六、选型指南

场景
推荐工具
命令示例
找命令位置
which
which python
找命令+手册+源码
whereis
whereis nginx
快速模糊找文件
locate
locate nginx.conf
精确条件找文件
find
find / -name "*.conf" -mtime -7
找大文件
find
find / -size +1G
找空文件/目录
find
find /tmp -empty
找 SUID 文件
find
find / -perm /u=s
批量操作文件
find
find . -name "*.log" -delete
找最近修改的文件
find
find . -mtime -1
找特定权限的文件
find
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 最快但有延迟。

最新文章

随机文章