1. 首页 > 电脑知识

`find` 命令的 15 种 高 质量用法:按权限、 时刻、内容搜索 find命令 -mtime

作者:admin 更新时间:2025-06-13
摘要:目录 1. 按权限搜索文件 2. 按用户/组搜索 3. 按时间搜索(修改/访问/变更时间) 4. 按分钟级别的时间搜索 5. 按文件大小搜索 6. 按文件内容搜索(结合grep) 7. 按文件类型搜索 8. 组合多个条件搜索 9. 排除特定目录 10. 执行操作(删除/更改权限等) 11. 按inode号搜索 12. 按文件名正则表达式搜索 13. 限制搜索深度 14. 按文件系统类型搜索 15.,`find` 命令的 15 种 高 质量用法:按权限、 时刻、内容搜索 find命令 -mtime

 

目录

1. 按权限搜索文件 2. 按用户/组搜索 3. 按 时刻搜索(修改/访问/变更 时刻) 4. 按分钟级别的 时刻搜索 5. 按文件 大致搜索 6. 按文件内容搜索(结合grep) 7. 按文件类型搜索 8. 组合多个条件搜索 9. 排除特定目录 10. 执行操作(删除/更改权限等) 11. 按inode号搜索 12. 按文件名正则表达式搜索 13. 限制搜索深度 14. 按文件 体系类型搜索 15. 高 质量内容搜索(结合多个工具) 性能对比表格

find 是 Linux/Unix 体系中功能最强大的文件搜索工具 其中一个,它不仅能按文件名搜索,还能根据文件权限、修改 时刻、内容等多种条件进行 高 质量搜索。 这篇文章小编将将介绍 15 种 find 命令的 高 质量用法,并提供性能对比数据。

1. 按权限搜索文件

# 查找权限为 4的文件 find /path -type f -perm 4 # 查找权限至少为755的文件 find /path -type f -perm -755 # 查找权限包含SUID位的文件 find /path -type f -perm /4000

2. 按用户/组搜索

# 查找属于用户user1的文件 find /path -user user1 # 查找属于组admin的文件 find /path -group admin # 查找不属于任何用户的文件 find /path -nouser

3. 按 时刻搜索(修改/访问/变更 时刻)

# 查找最近7天内修改过的文件 find /path -mtime -7 # 查找恰好7天前修改的文件 find /path -mtime 7 # 查找超过30天未访问的文件 find /path -atime +30 # 查找 情形改变在1小时内的文件 find /path -ctime -1

4. 按分钟级别的 时刻搜索

# 查找10分钟内修改过的文件 find /path -mmin -10 # 查找超过60分钟未访问的文件 find /path -amin +60

5. 按文件 大致搜索

# 查找大于10MB的文件 find /path -size +10M # 查找小于1KB的文件 find /path -size -1k # 查找 大致在1MB到10MB之间的文件 find /path -size +1M -size -10M

6. 按文件内容搜索(结合grep)

# 查找包含"error"的文 这篇文章小编将件 find /path -type f -name "*.txt" -exec grep -l "error" { } ; # 查找包含特定二进制模式的文件 find /path -type f -exec grep -l $'x00x01x02' { } ;

7. 按文件类型搜索

# 查找普通文件 find /path -type f # 查找目录 find /path -type d # 查找符号链接 find /path -type l # 查找套接字文件 find /path -type s

8. 组合多个条件搜索

# 查找属于user1且大于1MB的普通文件 find /path -type f -user user1 -size +1M # 查找7天内修改过但不是user1的文件 find /path -mtime -7 -not -user user1

9. 排除特定目录

# 查找文件但排除.git目录 find /path -name ".git" -prune -o -type f -print # 排除多个目录 find /path ( -name ".git" -o -name "node_modules" ) -prune -o -type f -print

10. 执行操作(删除/更改权限等)

# 删除所有.log文件 find /path -name "*.log" -delete # 更改文件权限为 4 find /path -type f -exec chmod 4 { } ; # 更改所有者为user1 find /path -exec chown user1:user1 { } ;

11. 按inode号搜索

# 查找特定inode号的文件 find /path -inum 12345 # 查找硬链接到指定文件的所有文件 find /path -samefile /path/to/file

12. 按文件名正则表达式搜索

# 使用正则表达式匹配文件名 find /path -regex ".*.(txt|pdf)$" # 不区分 大致写 find /path -iregex ".*.JPG$"

13. 限制搜索深度

# 只搜索当前目录 find /path - xdepth 1 -type f # 搜索最多3层子目录 find /path - xdepth 3 -type f

14. 按文件 体系类型搜索

# 只在ext4文件 体系中搜索 find /path -xdev -type f # 排除proc文件 体系 find /path -not -fstype proc

15. 高 质量内容搜索(结合多个工具)

# 查找包含"error"但不包含"warning"的PHP文件 find /path -name "*.php" -exec grep -l "error" { } ; | xargs grep -L "warning" # 查找包含特定模式的文件并统计出现次数 find /path -type f -exec grep -c "pattern" { } ; | awk -F: '$2 > 0 {print}'

性能对比表格

下面内容是在包含约100,000个文件的 体系上测试的不同搜索 技巧的性能对比(单位:秒):

搜索类型 命令示例 首次运行 缓存后运行 备注
按文件名 find / -name "*.log" 12.34 2.45 受文件 体系缓存影响大
按权限 find / -perm 4 15.67 3.21 需要检查每个文件的inode
按 大致 find / -size +1M 14.89 2.98 相对较快
按内容 find / -type f -exec grep "error" {} ; 125.78 120.45 最慢的 技巧
按 时刻 find / -mtime -7 13.45 2.67 性能接近文件名搜索
按用户 find / -user root 14.23 2.89 性能较好
组合条件 find / -user root -name "*.conf" 16.78 3.45 多个条件增加少量开销

优化建议:

尽可能缩小搜索范围(指定具体路径) 避免在根目录/下搜索 对内容搜索考虑先用locate缩小范围 复杂的多条件搜索可以分步进行

find 命令的强大之处在于其灵活的组合能力,掌握这些 高 质量用法可以极大 进步 体系管理和故障排查的效率。