find
find
find (1) - 递归地在层次目录中处理文件
语法
示例
指定查找类型
# 查找文件
find /path/to/directory -type f
# 查找目录
find /path/to/directory -type d
指定输出格式
# 输出相对查找目录的路径
find /path/to/directory -printf "%P\n"
# 输出文件大小
find /path/to/directory -printf "%s\n"
指定查找深度
# 查找最小深度
find /path/to/directory -mindepth 2
# 查找最大深度
find /path/to/directory -maxdepth 3
查找不符合条件的文件
# 查找不是目录的文件
find /path/to/directory -not -type d
查找指定名称的文件
# 查找指定名称的文件
find /path/to/directory -name "*.txt"
查找指定大小的文件
# 查找大于 100M 的文件
find /path/to/directory -size +100M
查找指定时间的文件
# 查找最近 1 天内修改的文件
find /path/to/directory -mtime -1
# 查找最近 1 天内访问的文件
find /path/to/directory -atime -1
# 查找最近 1 天内创建的文件
find /path/to/directory -ctime -1
# 查找最近 1 分钟内修改的文件
find /path/to/directory -mmin -1
# 查找最近 1 分钟内访问的文件
find /path/to/directory -amin -1
# 查找最近 1 分钟内创建的文件
find /path/to/directory -cmin -1
# 查找指定日期修改的文件
find /path/to/directory -mtime 2023-01-01
# 查找指定日期访问的文件
find /path/to/directory -atime 2023-01-01
# 查找指定日期创建的文件
find /path/to/directory -ctime 2023-01-01
# 查找修改时间大于 1 天,小于 30 天的文件
find /path/to/directory -mtime +1 -mtime -30
# 查找访问时间大于 1 天,小于 30 天的文件
find /path/to/directory -atime +1 -atime -30
# 查找创建时间大于 1 天,小于 30 天的文件
find /path/to/directory -ctime +1 -ctime -30
# 查找 2025 年 1 月 1 日后修改的文件
find /path/to/directory -newermt "2025-01-01"
# 查找 2025 年 1 月 1 日后访问的文件
find /path/to/directory -newerat "2025-01-01"
# 查找 2025 年 1 月 1 日后创建的文件
find /path/to/directory -newerc "2025-01-01"
组合条件
# 查找指定类型,指定名称,指定大小的文件
find /path/to/directory -type f -name "*.txt" -size +100M
# 查找类型不是文件,或者大小小于 100 字节的文件
find /path/to/directory -not \( -type f -and -size +100c \)
# 查找修改时间晚于 2025 年 1 月 1 日,但不晚于 2025 年 2 月 2 日的文件(即修改时间在 2025 年 1 月 1 日到 2025 年 2 月 2 日之间)
find /path/to/directory -newermt "2025-01-01" -not -newermt "2025-02-02"
执行命令
# 删除查找到的文件
find /path/to/directory -type f -name "*.txt" -delete
# 执行 rm 删除命令
find /path/to/directory -type f -name "*.txt" -exec rm -rf {} \;
参数
- delete
用于删除 find 命令匹配到的文件,默认不能删除非空目录,对符合条件的普通文件及空目录可直接删除 。