find命令

 

1.find名称查找

name 通过名称查找
[root@beua ~]# find ./ -name "*etho"
[root@beua ~]# find ./ -iname "*etho"

iname   通过名称查找(忽略大小写)

2.find大小查找 size 查找文件的大小 k\M\G
find /etc -size +5M     查找/etc目录下大于5M的文件
find /etc -size -5M      查找/etc目录小于5M的文件
find /etc -size 5M       查找/etc目录等于5M的文件

[root@beua ~]# find /etc -size +5M
/etc/udev/hwdb.bin

#查找/etc目录下文件大于5M,然后使用-ls参数以长格式显示(-ls和系统的ls不是一个命令)
[root@beua ~]# find /etc -size +5M -ls
71 7760 -r--r--r-- 1 root root 7942570 Mar 31 10:07 /etc/udev/hwdb.bin

3.find类型查找
f 文件
d 目录
l 链接
p 管道文件
s socket文件
c 字符设备
b 块设备

#查找/etc目录下文件大于5M,使用系统的ls来以长格式显示
[root@beua ~]# find /etc -size +5M |xargs ls -lh
-r--r--r--. 1 root root 7.6M Mar 31 10:07 /etc/udev/hwdb.bin
#查找当前目录下类型是文件的,并忽略大小写的名称查找为"*sh",都以长格式显示
[root@beua ~]# find ./ -type f -iname "*sh" -ls

查找当前目录下类型是目录的,并忽略大小写的名称查找为 "*sh",都以长格式显示
[root@beua ~]# find ./ -type d -iname "*sh" -ls                                                                                                                                                                                                                                                                
33575334 4 -rw-r--r-- 1 root root 374 Apr 17 11:39 ./vm.sh
#查找/bin下类型是链接文件的,忽略大小写查找名称为b*的,以长格式显示
[root@beua ~]# find /bin/ -type l -iname "b*" -ls

[root@beua ~]# find /dev/ -type b -ls
[root@beua ~]# find /dev/ -type c -ls

PS:类型有了,最好还有name或size,如下
[root@beua ~]# find /etc/ -type f -size +3M -name "hw*"

4.find时间查找
[root@beua ~]# for i in {01..28};do date -s 201904$i && touch file-$i;done


find ./ -iname "file-*" -mtime +7 查找7天以前
find ./ -iname "file-*" -mtime -7 查找最近7天的文件

+7,以当前时间为主,查找7天以前的内容(保留了最近7天的数据) 不会打印当天的文件
[root@beua ~]# find ./ -type f -name "file*" -mtime +7
./file.txt
[root@beua ~]# find ./ -type f -name "file*" -mtime +7 -delete

-7,查找最近7天的文件,不建议使用(会打印当天的文件)
[root@beua ~]# find ./ -type f -name "file*" -mtime -7

找第7天文件(不会打印当天的文件)
[root@beua ~]# find ./ -type f -mtime 7
./file-21

#本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件(实际使用方案)
[root@beua ~]# find /backup/ -iname "*.bak" -mtime +7 -delete
[root@beua ~]# find /backup/ -iname "*.bak" -mtime +90 -delete

5.find用户查找
#查找home目录下,类型是目录的并且属主是jack的,同时只查找一层
[root@beua ~]# find /home/ -maxdepth 1 -type d -user jack

#查找home目录下,类型是目录的并且属组是hr的,同时只查找一层
[root@beua ~]# find /home/ -maxdepth 1 -type d -group hr -ls

#查找home目录下,类型是目录的并且属主是jack属组是hr的,同时只查找一层
[root@beua ~]# find /home/ -maxdepth 1 -type d -user jack -group hr -ls

#查找home目录下,类型是目录的要么属主是jack,要么属组是hr
[root@beua ~]# find /home/ -maxdepth 1 -type d -user jack -o -group hr|xargs ls -ld

#查找home目录下,类型是目录没有属主的
[root@beua ~]# find /home/ -maxdepth 1 -type d -nouser -ls
#查找home目录下,类型是目录没有属组的
[root@beua ~]# find /home/ -maxdepth 1 -type d -nogroup -ls

#查找home目录下,类型是目录没有属主或没有属组
[root@beua ~]# find /home/ -maxdepth 1 -type d -nouser -o -nogroup |xargs ls -ld


6.find权限查找
精确查找文件的权限为644
[root@beua ~]# find ./ -type f -perm 644

#包含444权限即可 -444
[root@beua ~]# find ./ -type f -name "file*" -perm -444

#查找全局可写(每位权限必须包含w)
[root@beua ~]# find . -perm -222 -ls
#包含set uid
[root@beua ~]# find /usr/sbin -perm -4000 -ls
#包含set gid
[root@beua ~]# find /usr/sbin -perm -2000 -ls
#包含sticky
[root@beua ~]# find /usr/sbin -perm -1000 -ls

#查找/etc目录下至少有一类用户没有写权限的文件
[root@beua /]# find /etc/ -type f ! -perm -222 -ls
#查找/etc目录下所有用户都没有写权限的文件
[root@beua /]# find /etc/ -type f ! -perm /222 -ls

Action动作:
-delte,只能删除文件,如果要删除目录,需要保证目录为空,否则无法删除
[root@beua ~]# find ./log/ -type f -name "*.log" -delete

-ok,可以执行任何自定义命令,但是会提示是否确认.
[root@beua ~]# find /etc/ -name "ifcfg*" -ok cp -vp {} /tmp \;

-exec      是将查找到的文件,一个一个删除
[root@beua ~]# find /etc/ -name "ifcfg*" -exec cp -vp {} /tmp \;
[root@beua ~]# find log/ -type d -exec cp -rpv {} /tmp \;
[root@beua ~]# find test/ -type f -exec rm -f {} \;


#xargs将前者命令查找到的文件作为一个整体传递后者命令的输入
[root@beua ~]# touch file.txt
[root@beua ~]# find . -name "file.txt" |xargs rm -f
[root@beua ~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /tmp

5.find用户查找
#查找home目录下,类型是目录的并且属主是jack的,同时只查找一层
[root@beua ~]# find /home/ -maxdepth 1 -type d -user jack

#查找home目录下,类型是目录的并且属组是hr的,同时只查找一层
[root@beua ~]# find /home/ -maxdepth 1 -type d -group hr -ls

#查找home目录下,类型是目录的并且属主是jack属组是hr的,同时只查找一层
[root@beua ~]# find /home/ -maxdepth 1 -type d -user jack -group hr -ls

#查找home目录下,类型是目录的要么属主是jack,要么属组是hr
[root@beua ~]# find /home/ -maxdepth 1 -type d -user jack -o -group hr|xargs ls -ld

#查找home目录下,类型是目录没有属主的
[root@beua ~]# find /home/ -maxdepth 1 -type d -nouser -ls
#查找home目录下,类型是目录没有属组的
[root@beua ~]# find /home/ -maxdepth 1 -type d -nogroup -ls

#查找home目录下,类型是目录没有属主或没有属组
[root@beua ~]# find /home/ -maxdepth 1 -type d -nouser -o -nogroup |xargs ls -ld


6.find权限查找
精确查找文件的权限为644
[root@beua ~]# find ./ -type f -perm 644

#包含444权限即可 -444
[root@beua ~]# find ./ -type f -name "file*" -perm -444

#查找全局可写(每位权限必须包含w)
[root@beua ~]# find . -perm -222 -ls
#包含set uid
[root@beua ~]# find /usr/sbin -perm -4000 -ls
#包含set gid
[root@beua ~]# find /usr/sbin -perm -2000 -ls
#包含sticky
[root@beua ~]# find /usr/sbin -perm -1000 -ls

查找/etc目录下至少有一类用户没有写权限的文件
[root@beua /]# find /etc/ -type f ! -perm -222 -ls
查找/etc目录下所有用户都没有写权限的文件
[root@beua /]# find /etc/ -type f ! -perm /222 -ls

Action动作:
-delte,只能删除文件,如果要删除目录,需要保证目录为空,否则无法删除
[root@beua ~]# find ./log/ -type f -name "*.log" -delete

-ok,可以执行任何自定义命令,但是会提示是否确认.
[root@beua ~]# find /etc/ -name "ifcfg*" -ok cp -vp {} /tmp \;

-exec
[root@beua ~]# find /etc/ -name "ifcfg*" -exec cp -vp {} /tmp \;
[root@beua ~]# find log/ -type d -exec cp -rpv {} /tmp \;
[root@beua ~]# find test/ -type f -exec rm -f {} \;


#xargs将前者命令查找到的文件作为一个整体传递后者命令的输入
[root@beua ~]# touch file.txt
[root@beua ~]# find . -name "file.txt" |xargs rm -f
[root@beua ~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /tmp

!    #取反

查找/tmp目录下,属主不是root,且文件名不以c开头的文件
[root@beua ~]# find /tmp/ ! -user root ! -name "c*"
/tmp/ttt

 

posted @ 2019-11-19 16:44  Beua  阅读(346)  评论(0编辑  收藏  举报