find 查找命令
find命令的语法
find: 只查找文件
grep: 筛选文件里面的内容
1.find名称查找
[root@chenpeng ~]# find ./ -name "*eth0" #查找当前目录下名称为 *eth0
[root@chenpeng ~]# find ./ -iname "*eth0" #忽略大小写查找
2.find大小查找
#查找/etc目录下文件大于5M,然后使用-ls参数以长格式显示(-ls和系统的ls不是一个命令)
[root@chenpeng ~]# find /etc -size +5M -ls
#查找/etc目录下文件大于5M,使用系统的ls来以长格式显示
[root@chenpeng ~]# find /etc -size +5M |xargs ls -lh
查找/etc目录下小于1b的文件
[root@chenpeng ~]# find /etc -size -1b|xargs ls -l
3.find类型查找
f 文件
d 目录
l 链接
p 管道文件
s socket文件
c 字符设备
b 块设备
查找当前目录下类型是文件的,并忽略大小写的名称查找为 *-eth0,都以长格式显示
[root@chenpeng ~]# find ./ -type f -iname "*-eth0" -ls
查找当前目录下类型是目录的,并忽略大小写的名称查找为 *-eth0,都以长格式显示
[root@chenpeng ~]# find ./ -type d -iname "*-eth0" -ls
查找/bin下类型是链接文件的,忽略大小写查找名称为b*的,以长格式显示
[root@chenpeng ~]# find /bin/ -type l -iname "b*" -ls
[root@chenpeng ~]# find /dev/ -type b -ls
[root@chenpeng ~]# find /dev/ -type c -ls
PS:类型有了,最好还有name或size,如下
[root@chenpeng ~]# find /etc/ -type f -size +3M -name "hw*"
4.find时间查找
创建 28天文件,每天一个
[root@chenpeng ~]# for i in {01..28};do date -s 201904$i && touch file-$i;done
+7,以当前时间为主,查找7天以前的内容(保留了最近7天的数据) 不会打印当天的文件
[root@chenpeng ~]# find ./ -type f -name "file*" -mtime +7
[root@chenpeng ~]# find ./ -type f -name "file*" -mtime +7 -delete
-7,查找最近7天的文件,不建议使用(会打印当天的文件)
[root@chenpeng ~]# find ./ -type f -name "file*" -mtime -7
找第7天文件(不会打印当天的文件)
[root@chenpeng ~]# find ./ -type f -mtime 7
#本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件(实际使用方案)
[root@chenpeng ~]# find /backup/ -iname "*.bak" -mtime +7 -delete
[root@chenpeng ~]# find /backup/ -iname "*.bak" -mtime +90 -delete
5.find用户查找
#查找home目录下,类型是目录的并且属主是jack的,同时只查找一层
[root@chenpeng ~]# find /home/ -maxdepth 1 -type d -user jack
#查找home目录下,类型是目录的并且属组是hr的,同时只查找一层
[root@chenpeng ~]# find /home/ -maxdepth 1 -type d -group hr -ls
#查找home目录下,类型是目录的并且属主是jack属组是hr的,同时只查找一层
[root@chenpeng ~]# find /home/ -maxdepth 1 -type d -user jack -group hr -ls
#查找home目录下,类型是目录的要么属主是jack,要么属组是hr
[root@chenpeng ~]# find /home/ -maxdepth 1 -type d -user jack -o -group hr|xargs ls -ld
#查找home目录下,类型是目录没有属主的
[root@chenpeng ~]# find /home/ -maxdepth 1 -type d -nouser -ls
#查找home目录下,类型是目录没有属组的
[root@chenpeng ~]# find /home/ -maxdepth 1 -type d -nogroup -ls
#查找home目录下,类型是目录没有属主或没有属组
[root@chenpeng ~]# find /home/ -maxdepth 1 -type d -nouser -o -nogroup |xargs ls -ld
6.find权限查找
精确查找文件的权限为644
[root@chenpeng ~]# find ./ -type f -perm 644
#包含444权限即可 -444
[root@chenpeng ~]# find ./ -type f -name "file*" -perm -444
#查找全局可写(每位权限必须包含w)
[root@chenpeng ~]# find . -perm -222 -ls
#包含set uid
[root@chenpeng ~]# find /usr/sbin -perm -4000 -ls
#包含set gid
[root@chenpeng ~]# find /usr/sbin -perm -2000 -ls
#包含sticky
[root@chenpeng ~]# find /usr/sbin -perm -1000 -ls
Action动作:
-delte,只能删除文件,如果要删除目录,需要保证目录为空,否则无法删除
[root@chenpeng ~]# find ./log/ -type f -name "*.log" -delete
-ok,可以执行任何自定义命令,但是会提示是否确认.
[root@chenpeng ~]# find /etc/ -name "ifcfg*" -ok cp -vp {} /tmp \;
-exec
[root@chenpeng ~]# find /etc/ -name "ifcfg*" -exec cp -vp {} /tmp \;
[root@chenpeng ~]# find log/ -type d -exec cp -rpv {} /tmp \;
[root@chenpeng ~]# find test/ -type f -exec rm -f {} \;
#xargs将前者命令查找到的文件作为一个整体传递后者命令的输入
[root@chenpeng ~]# touch file.txt
[root@chenpeng ~]# find . -name "file.txt" |xargs rm -f
[root@chenpeng ~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /tmp
#1.测试exec和xargs的删除速度
[root@chenpeng ~]# find ./|wc -l
18034
#2.exec是将查找到的文件,一个一个删除
[root@chenpeng ~]# time find ./ -exec rm -rf {} \;
real 0m0.960s
user 0m0.051s
sys 0m0.880s
#3.xargs是将文件整体作为一个目标,一次删除
[root@chenpeng ~]# time find ./ |xargs rm -fr
real 0m0.514s
user 0m0.047s
sys 0m0.478s
find:
1.按照名称查找\大小查找\文件类型查找\时间查找\用户组查找\权限查找
2.查找之后的文件使用什么动作进行处理