find_linux_command

find 命令

根据文件名查找

1
2
3
4
5
6
7
8
9
10
#-name 跟文件名 可用通配符
#-iname 忽略大小写

#-regex 跟文件路径字符串
#-iregex 忽略大小写

#-prune 不寻找字符串作为范本样式

#在当前目录查找test开头 sh结尾的文件
find ./ -iname "test*.sh"

根据属主 属组查找

1
2
3
4
5
6
7
8
9
#-user username
#-group groupname
#-uid UserID
#-gid GroupID
#-nouser 没有属主
#-nogroup 没有属组

#在当前文件查找属主是root 以pass开头的文件
find ./ -user root -name "pass*"

根据文件类型查找

1
2
3
4
#-type f普通文件 d目录文件 l符号链接 s套接字文件 b块设备文件 c字符设备文件 p管道文件

#在当前目录查找myDemo开头的目录文件
find ./ -type d -name "myDemo*"

组合条件

1
2
3
#-a and与
#-o or或
#-not -! 非

根据文件大小查找

1
2
3
4
#-size b c w k M G

#在当前目录查找文件大小大于10M小于10G的普通文件
find ./ -type f -size -10G -size +10M

根据时间戳查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#以天为单位
#-atime 最近访问时间
#-mtime 最近修改文件内容时间
#-ctime 最近修改元数据时间

#以分钟为单位
#-amin
#-mmin
#-cmin

#高级
#-daystart 从本日开始计算时间
#-newer <参考文件或目录> 查找其更改时间更接近现在的文件或目录
#-anewer <参考文件或目录>
#-cnewer <参考文件或目录>

#在当前目录查找比new文件修改时间更新的文件或目录
find ./ -type f -newer new

根据权限查找

1
2
3
4
#-perm 4-r 2-w 1-x

#在当前目录查找可读可写的目录
find ./ -type d -perm 555

符号链接处理

1
2
3
4
5
6
7
#-follow 排除符号链接
#-links <链接数目> 查找符合指定的硬链接数目的文件或目录
#-inum <inode 编号> 查找符合指定的inode编号的文件或目录
#-noleaf 不去考虑目录至少需要拥有两个硬链接存在

#在当前目录排除符号链接查找含有pass的普通文件
find ./ -type f -follow -name "*pass*"

查找深度

1
2
3
4
5
6
7
8
#-path <范本样式> 指定字符串做为寻找目录的范本样式
#-ipath 忽略大小写
#-mindepth <目录层级> 设置最小目录层级
#-maxdepth <目录层级> 设置最大目录层级
#-depth 从指定目录下最深层的子目录开始查找

#在当前目录3层深度内查找10天内访问过的普通文件
find ./ -type f -atime -10 -maxdepth 3

处理动作

1
2
3
4
5
6
7
8
9
10
11
#-print 默认处理动作  显示岛屏幕
#-print0 将文件或目录名称列出岛标准输出,格式为全部的名称全在同一行
#-printf <输出格式> 自行设定输出格式
#-ls 类似对查找盗的文件执行“ls -l”命令
#-delete 刹车农户查找盗的文件
#-fls /path/to/somefile 查找到所有文件的长格式信息保存至指定文件种
#-ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令 每次都会要求用户确认
#-exec COMMAND {} \; 不需要确认

#在当前目录下对所有包含pass的普通文件执行ls -lh命令
find ./ -type f -exec ls -lh {} \; -name "*pass*"