linux - find 查找文件

发布时间 2023-11-26 16:05:45作者: 箫笛

1. -name 在当前目录以及子目录中根据文件名进行查找

find -name "apt.md"

2. -iname 忽略大小写进行查找

find -iname "apt.md"

3. -mindepth, -maxdepth 设置从根目录下查找的目录层级

find / -mindepth 3 -maxdepth 5 -name passwd

4. -exec 对查找到的文件执行命令

find -name '*.md' -exec md5sum {} \;

5. -not 显示不匹配的查找结果

find -not -iname "apt.md"

6. -inum 通过文件的inode值进行查找并执行命令

ls -il  // 查看文件inode 值
find -inum 1187273 -exec rm {} \;

7. -perm 根据分组具有读权限进行查找

find . -perm -g=r -type f -exec ls -l {} \;

8. -empty 在用户目录查找空文件

find ~ -empty

9. 在当前目录查找最大的5个文件和最小的5个文件

# 查找最大5个文件
find . -type f -exec ls -s {} \; | sort -n -r | head -5
# 查找最小5个文件
find . -type f -exec ls -s {} \; | sort -n  | head -5
# 查找最小5个文件并且非空文件
find . -not -empty -type f -exec ls -s {} \; | sort -n  | head -5

10. 基于文件类型进行查找

# 查找目录
find . -type d
# 查找普通文件
find . -type f
# 查找所有隐藏的文件
find . -type f -name ".*"

11. 基于文件的修改时间进行查找

find -newer ordinary_file

12. 基于文件大小进行查找

find ~ -size +100M # 大于100M
find ~ -size -100M # 小于100M
find ~ -size 100M # 等于100M