一.功能
在指定目录查找符合条件的文件
二.语法
find [路径] [选项] [表达式]
三.常用选项
选项 功能 -name 根据文件名查找 (’ * ', ) -type 根据文件类型查找 (详细类型在后) -perm 根据文件权限查找,比如 777 -user 根据属主查找 -group 根据属组查找 -size 根据文件大小 -maxdepth n 最大搜索层数 (n:数字) -o 或者 -a 并且(默认就是) -not 表达式: 非
f
d
l
b
C
s
p
b
w
c
K
M
G
三.查找文件
1.按文件名查找
find / etc - name 'ifcfg-ens32'
find / etc - iname 'ifcfg-ens32'
find / etc - iname 'ifcf*'
2.按文件大小
find / etc - size + 3M
find / etc - size 3M
find / etc - size - 3M
find / etc - size + 3M - ls
3.指定查找目录的深度
用法: - maxdepth [ 指定层数]
find / etc - maxdepth 5 - name "ifcfg.*"
4.按时间查找 (atime, mtime, ctime)
find / etc - mtime + 3
find / etc - mtime 3
find / etc - mtime - 3
5.按属组查找
find / root - user shawn
find / root - group song
find / root - user shawn - group song
find / root - user shawn - a - group - song
find / root - user shawn - o - group - song
语法 : chown [ user] . [ grep] a. txt
用法 : 将"a.txt" 的属主和属组都变成"root"
删除一个文件的属主: 进入"/etc/passwd" 下删除这个用户记录
删除一个文件的属组: 进入"/etc/group" 下删除这个用户所在的组记录
find / root - nouser
find / root - nogroup
find / root - nouser - o nogroup
6.按文件类型查找
find / root - type f
find / root - type d
find / root - type l
find / root - type c
find / root - type b
find / root - type s
find / root - type p
7.按文件权限查找 (后续用户权限详细讲)
find / root - perm 644 - print
find / root - perm - 644 - ls
find / root - perm - 600 - ls
四.找到文件的后续处理
举例命令
命令 作用 -print 默认命令,打印找到的文件 -ls 显示详细信息 -delete 删除 -exec 每次操作不提醒直接执行 -ok 每次操作进行提醒 “y/n”
find / root - name "song*" - print
find / root - name "song*" - ls
find / root - name "song*" - delete
find / root - name "song*" - exec rm - rf { } \;
配合 “-exec” 和 “-ok” 进行交互与非交互
find / root - name "song*" - ok cp - rvf { } / tmp \;
find / root - name "song*" - exec cp - rvf { } / tmp \;
五.find 与 xargs 配合使用
1.xargs
作用
find / root - name "song*" | xargs rm - rvf
find / root - name "song*" | xargs - I { } cp - rf { } / tmp
find / root - name "song*" | xargs - I { } mv { } / tmp
find / root - name "song*" | xargs - I { } chmod 777 { }