Linux下20个find命令
1.按文件名精确查找
1
| find /var/log -name "*.log"
|
描述:在/var/log目录查找所有.log后缀文件
1 2
| /var/log/syslog /var/log/auth.log
|
2.忽略大小写搜索
1
| find /etc -iname "*.CONF"
|
描述:查找配置文件,不区分大小写
3.按类型查找目录
描述:列出/home下所有子目录
1 2
| /home/user/Documents /home/user/Downloads
|
4.多条件组合查找(AND)
1
| find /tmp -name "temp*" -type f
|
描述:查找以temp开头的普通文件
5.多条件组合查找(OR)
1
| find . \( -name "*.jpg" -o -name "*.png" \)
|
描述:查找当前目录所有图片文件
6.排除特定目录
1
| find /app -path "/app/cache" -prune -o -name "*.conf"
|
描述:跳过cache目录查找配置文件
7.按时间查找文件
1
| find ~/Documents -mtime -1
|
描述:查找24小时内修改过的文档
1
| /home/user/Documents/report.txt
|
8.按大小查找文件
描述:查找大于10MB的大文件
1
| /var/lib/docker/largefile.bin
|
9.按权限精确查找
描述:查找权限为644的文件
1
| ./public_html/index.html
|
10.查找空文件/目录
描述:查找0字节文件或空目录
11.按属主查找文件
1
| find /var/www -user www-data
|
描述:查找属主为www-data的文件
12.安全删除旧文件
1
| find . -name "*.tmp" -mtime +30 -exec rm -v {} \;
|
描述:删除30天前的临时文件
1
| removed './cache/old.tmp'
|
13.查找并列出文件详情
1
| find /etc/nginx -name "*.conf" -exec ls -l {} \;
|
描述:显示配置文件详细信息
1
| -rw-r--r-- 1 root root 2652 /etc/nginx/nginx.conf
|
14.查找符号链接
描述:列出所有软链接文件
15.查找硬链接文件
1
| find /home -samefile important.txt
|
描述:查找指向同一inode的文件
1
| /home/backups/important.txt
|
16.限制搜索深度
1
| find /var -maxdepth 2 -name "log"
|
描述:只搜索两层子目录
17.查找SUID权限文件
1
| find /usr/bin -perm /4000
|
描述:查找有SUID位的程序
18.查找可执行文件
1
| find ~/scripts -executable
|
描述:查找有执行权限的脚本
1
| /home/user/scripts/backup.sh
|
19.按内容搜索文件
1
| find . -type f -exec grep -l "ERROR" {} \;
|
描述:查找包含”ERROR”的文件
20.查找并归档文件
1
| find /var/log/nginx -name "access.*.gz" -mtime +90 -exec tar -rvf old_logs.tar {} \;
|
描述:归档90天前的日志
1
| added access.log-20230101.gz
|
运维安全提示:
- 使用
-exec前先用-print预览结果
- 删除操作建议用
-ok替代-exec进行确认
- 根目录搜索添加
2>/dev/null过滤错误