4 个查找命令

1. find — 实时按条件遍历

最强、最常用。实时遍历文件系统,灵活但稍慢。

find /etc -name "*.conf"               # 按名字找
find . -type f -name "*.log"           # 只找文件(type f)
find /home -type d -name "tmp"         # 只找目录(type d)
find /var -size +100M                  # 大于 100M 的文件
find /var/log -mtime -7                # 7 天内修改过
find . -mmin -60                       # 60 分钟内修改过
find . -user wadely                    # 属主是 wadely
find /tmp -name "*.tmp" -delete        # 找到并删

组合条件:

# 找最近 1 天改的 .log 文件
find /var/log -name "*.log" -mtime -1

# 找大于 1G 的文件并执行命令
find / -size +1G -exec ls -lh {} \;

2. locate — 用预建索引秒搜

sudo apt install plocate    # 装一下
sudo updatedb               # 建索引(cron 每天自动更新)
locate nginx.conf           # 闪电级查找

适合:已知名字 + 想快的场景。缺点:索引可能不是最新的。

3. which — 看命令在哪

which python3
# /usr/bin/python3

which ls
# /usr/bin/ls

适合:确认你跑的是哪个版本——$PATH 里可能有多个同名命令。

4. whereis — 命令 + 文档 + 源码

whereis ls
# ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

which 多给一个 man page 路径。

决策矩阵

场景 用哪个
知道大概名字,找文件 findlocate
找命令在哪 which
找命令 + 帮助 whereis
实时找 / 复杂条件 find
简单按名搜 locate

find 常用模板

# 1. 清理 30 天前的日志
find /var/log -name "*.log" -mtime +30 -delete

# 2. 找空文件
find . -type f -empty

# 3. 找最近改过的代码
find . -name "*.py" -mtime -1

# 4. 找权限太开的文件(安全审计)
find /etc -type f -perm 777

# 5. 按属主查
find /home -user old-user -ls

下一篇:找到文件之后,怎么在文件里搜内容——grep + 管道。