一、命令简介
find
命令的作用是搜索文件和目录。
相关命令:locate、whereis
二、命令参数
find [起始目录] [匹配模式] [条件]
选项和参数
-
-name pattern
: 根据文件名模式查找文件 -
-type type
: 根据文件类型查找文件(如f
表示普通文件,d
表示目录) -
-exec command {} \;
: 对查找到的文件执行指定的命令 -
-size [+/-]size
: 根据文件大小查找文件(可使用c
表示字节,k
表示KB,M
表示MB) -
-mtime n
: 根据文件修改时间查找文件(n
表示天数) -
-delete
: 删除查找到的文件☢️
三、命令示例
按文件名查找:
find /path/to/directory -name "filename"
按文件类型查找:
find /path/to/directory -type f # 查找文件 find /path/to/directory -type d # 查找目录
按权限查找:
find /path/to/directory -perm 644 # 查找权限为644的文件
按大小查找:
find /path/to/directory -size +1M # 查找大于1MB的文件
按时间查找:
find /path/to/directory -mtime -7 # 查找最近7天内修改过的文件
组合条件:
find /path/to/directory -name "*.txt" -type f -size +100k # 查找大于100KB的txt文件
执行操作:
find /path/to/directory -name "*.log" -exec rm {} \; # 删除所有以.log结尾的文件
查找目录并排除特定目录:
find /path/to/directory -type d -name "dir*" -not -path "*/dir_to_exclude/*"
按深度查找:
find /path/to/directory -maxdepth 2 -type f # 限制查找深度为2层
本文共 250 个字数,平均阅读时长 ≈ 1分钟
评论 (0)