一、find(最强大、最常用)
基本语法
find [搜索路径] [匹配条件] [执行动作]
按名称查找
# 精确匹配文件名
find / -name "test.txt"
# 忽略大小写
find /home -iname "Readme.md"
# 通配符匹配
find /var -name "*.log"
find /etc -name "nginx*"
find /tmp -name "???.txt" # 匹配3个字符的文件名
按类型查找
find / -type f -name "*.conf" # 普通文件
find / -type d -name "logs" # 目录
find / -type l -name "*.link" # 符号链接
find / -type b # 块设备
find / -type c # 字符设备
find / -type s # 套接字
find / -type p # 命名管道
按大小查找
find / -size +100M # 大于 100MB
find / -size -1M # 小于 1MB
find / -size 50M # 恰好 50MB
find / -size +1G -size -10G # 1GB ~ 10GB 之间
find / -empty # 空文件/空目录
按时间查找
# 修改时间(内容修改)
find / -mtime -7 # 7天内修改的
find / -mtime +30 # 30天前修改的
find / -mtime 1 # 恰好1天前(昨天)
# 访问时间
find / -atime -1 # 1天内访问过的
# 变更时间(元数据变化)
find / -ctime -7 # 7天内
# 按分钟(更精确)
find / -mmin -30 # 30分钟内修改的
find / -mmin +60 # 60分钟前修改的
按权限/用户查找
find / -perm 755 # 权限恰好 755
find / -perm -644 # 至少包含 644 权限
find / -perm /u+w # 用户有写权限
find / -user john # 属主为 john
find / -group www # 属组为 www
find / -nouser # 无属主的文件
find / -nogroup # 无属组的文件
按目录深度查找
find / -maxdepth 3 -name "*.py" # 最多搜索3层目录
find / -mindepth 2 -name "*.py" # 至少从第2层开始搜索
组合条件
# AND(默认)
find / -name "*.log" -size +100M
# OR
find / -name "*.log" -o -name "*.txt"
# NOT
find / -name "*.log" ! -name "debug.log"
# 复杂组合
find /var $$ -name "*.log" -o -name "*.tmp" $$ -size +10M
find / -type f -name "*.bak" -mtime +90 ! -empty
执行动作
# 删除匹配的文件
find /tmp -name "*.tmp" -mtime +7 -delete
# 执行命令({} 代表匹配的文件)
find / -name "*.log" -exec gzip {} \;
find / -name "*.log" -exec rm -f {} \;
find / -name "*.conf" -exec grep -l "port" {} \;
# 批量执行(效率更高,用 + 代替 \;)
find / -name "*.log" -exec gzip {} +
# 打印详细信息
find /home -name "*.sh" -ls
# 确认后删除(交互式)
find /tmp -name "*.tmp" -ok rm {} \;
二、locate(快速全盘搜索)
# 安装 sudo yum install mlocate # CentOS sudo apt install plocate # Ubuntu # 更新数据库 sudo updatedb # 搜索 locate test.txt locate -i "readme.md" # 忽略大小写 locate -c "*.log" # 只显示数量 locate -l 10 "*.conf" # 限制结果数量 locate -r "nginx.*\.conf$" # 正则匹配
对比 find :locate 搜索的是预建数据库,速度极快,但新文件需先
updatedb。
三、which / whereis / type
# 查找可执行文件路径(在 $PATH 中搜索)
which nginx
which python3
# 查找二进制文件、源码、手册页
whereis nginx
# 输出: nginx: /usr/sbin/nginx /etc/nginx /usr/share/man/man8/nginx.8.gz
# 查找命令类型(内置/别名/外部)
type cd # cd is a shell builtin
type ls # ls is aliased to 'ls --color=auto'
type nginx # nginx is /usr/sbin/nginx
四、grep(在文件内容中搜索)
# 在文件中搜索文本
grep "error" /var/log/syslog
grep -r "password" /etc/ # 递归搜索目录
grep -rn "TODO" /home/ # 显示行号
grep -rl "localhost" /etc/ # 只显示文件名
# 高级选项
grep -i "error" log.txt # 忽略大小写
grep -w "port" config.txt # 全词匹配
grep -c "error" log.txt # 统计匹配行数
grep -A 3 "error" log.txt # 显示匹配行后3行
grep -B 3 "error" log.txt # 显示匹配行前3行
grep -C 3 "error" log.txt # 显示匹配行前后各3行
grep -v "debug" log.txt # 反向匹配(排除)
# 正则表达式
grep -E "error|warn|fatal" log.txt # 扩展正则
grep -P "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" log.txt # Perl 正则(IP地址)
五、awk / sed(高级文本搜索)
# awk 按列/条件筛选
awk '/error/ {print $0}' /var/log/syslog
awk '$3 > 1000' data.txt # 第3列大于1000
awk -F: '/bash$/ {print $1}' /etc/passwd # 搜索使用bash的用户
# sed 搜索并替换
sed -n '/error/p' log.txt # 打印匹配行
sed -n '100,200p' largefile.txt # 打印100~200行
六、其他实用查找工具
fd(find 的现代替代品)
# 安装
sudo apt install fd-find # Ubuntu
sudo yum install fd-find # CentOS
# 使用
fd "test"
fd -e py # 按扩展名查找 .py 文件
fd -H ".bashrc" # 包含隐藏文件
fd -t d "log" # 只搜索目录
fzf(模糊搜索)
# 安装
sudo apt install fzf
# 交互式搜索
fzf # 搜索当前目录
find / -name "*.log" | fzf # 管道交互式筛选
stat(查看文件详细元数据)
stat /etc/nginx/nginx.conf
stat /etc/nginx/nginx.conf
file(判断文件类型)
file /tmp/mystery.dat
file -b /tmp/mystery.dat # 只输出类型描述
file /tmp/mystery.dat file -b /tmp/mystery.dat # 只输出类型描述
七、常用实战场景速查
| 场景 | 命令 |
|---|---|
| 查找大于 1G 的文件 | find / -type f -size +1G 2>/dev/null |
| 查找最近 1 天修改的文件 | find /var -type f -mtime -1 |
| 删除 7 天前的日志 | find /var/log -name "*.log" -mtime +7 -delete |
| 查找权限为 777 的文件 | find / -perm 0777 -type f |
| 查找属主为 root 的 SUID 文件 | find / -user root -perm -4000 |
| 查找并统计数量 | `find / -name "*.conf" |
| 查找空文件/目录 | find / -empty -type f |
| 查找被删除但仍占空间的文件 | `lsof |
| 查找超过 100M 的文件并排序 | `find / -size +100M -exec ls -lh {} + 2>/dev/null |
| 查找两个文件间的差异 | diff file1 file2 |
| 快速定位可执行文件 | which nginx |
| 内容搜索含 IP 的行 | grep -rnE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" /var/log/ |