根据文件名称查找
选项:
-name #根据名称查找
-i #忽略大小写 -iname
通配符
* #所有
? #匹配任意一个字符 只能匹配一个
[] #匹配中括号中的任意一个字符
[^] #排除中括号中的字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#精确查找 [root@qls ~]# find / -name "hostnamectl" #查找以什么开头的 [root@qls ~]# find / -name "hostnamectl*" #以什么为结尾的 [root@qls ~]# find / -name "*hostnamectl" #查找包含某个字符串的查找 [root@qls ~]# touch /tmp/rrhostnamectlfg [root@qls ~]# find / -name "*hostnamectl*" #忽略大小写 [root@qls ~]# find / -iname "*hostnamectl*" |
根据文件类型查找
选项:
-type #根据类型进行查找
f #普通文件
d #目录
l #软链接
s #套接字 socket文件
p #管道文件
b #块设备 硬盘 硬盘分区 镜像光盘
c #字符设备
1 2 3 4 5 6 7 8 |
#查找所有的普通文件,包括隐藏文件 [root@qls ~]# find /root -type f #所有的目录 包括隐藏目录 也包含自己的本身 [root@qls ~]# find /root -type d #tree命令会把软链接也统计成目录 软链接的源文件是目录 find不会 [root@qls ~]# tree -ad /root |
根据文件的大小查找
选项:
-size #根据文件大小查找
+n #大于
-n #小于
n #精确匹配
b k M G #单位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#查找空文件 [root@qls ~]# find /var/log/ -size 0b /var/log/tallylog /var/log/anaconda/ks-script-3PCKae.log [root@qls ~]# ll /var/log/tallylog -rw-------. 1 root root 0 Jul 6 02:13 /var/log/tallylog [root@qls ~]# find /var/log/ -size -1k -ls 67159586 0 -rw------- 1 root root 0 Jul 6 02:13 /var/log/tallylog 67563450 0 -rw------- 1 root root 0 Jul 6 02:17 /var/log/anaconda/ks-script-3PCKae.log 67563454 0 -rw------- 1 root root 0 Jul 27 [root@qls ~]# find /var/log/ -size 1k -ls #字节的大小在find中四舍五入 67160720 4 -rw-r--r-- 1 root root 193 Jul 6 02:13 /var/log/grubby_prune_debug 681834 0 drwxr-xr-x 2 root root 23 Jul 6 02:17 /var/log/tuned 714023 0 drwx------ 2 root root 23 Jul 6 02:17 /var/log/audit [root@qls ~]# find /var/log/ -type f -size 20k /var/log/cron-20200726 [root@qls ~]# find /var/log/ -type f -size 20k -ls 67588776 20 -rw------- 1 root root 19815 Jul 26 03:33 /var/log/cron-20200726 [root@qls ~]# find /var/log/ -type f -size 6k -ls 75 8 -rw-r--r-- 1 root root 6085 Jul 27 16:29 /var/log/tuned/tuned.log #根据范围进行查找 [root@qls ~]# find /var/log/ -type f -size +100k -size -1000k /var/log/anaconda/syslog /var/log/anaconda/packaging.log [root@qls ~]# find /var/log/ -type f -size +100k -size -1000k -ls 67496291 276 -rw------- 1 root root 281510 Jul 6 02:17 /var/log/anaconda/syslog 67563447 112 -rw------- 1 root root 110647 Jul 6 02:17 /var/log/anaconda/packaging.log #四舍五入 1k [root@qls ~]# find /var/log/ -type f -size 5k /var/log/vmware-network.8.log [root@qls ~]# find /var/log/ -type f -size 5k -ls 67112873 8 -rw-r--r-- 1 root root 4127 Jul 6 09:05 /var/log/vmware-network.8.log #以M为单位查找时,所有小于1M的文件都四舍五入到1M [root@qls ~]# find /var/log/ -type f -size 1M -ls 67160720 4 -rw-r--r-- 1 root root 193 Jul 6 02:13 /var/log/grubby_prune_debug 67284922 48 -rw-rw-r-- 1 root utmp 45696 Jul 27 16:30 /var/log/wtmp [root@qls ~]# find /var/log/ -type f -size -1M -ls 67159586 0 -rw------- 1 root root 0 Jul 6 02:13 /var/log/tallylog 67563450 0 -rw------- 1 root root 0 Jul 6 02:17 /var/log/anaconda/ks-script-3PCKae.log [root@qls ~]# find /var/log/ -type f -size 2M -ls 67263633 40 -rw-r--r-- 1 root root 1946764 Jul 27 16:30 /var/log/lastlog 714345 1436 -rw------- 1 root root 1468869 Jul 27 18:01 /var/log/audit/audit.log [root@qls ~]# ll -h /var/log/lastlog -rw-r--r--. 1 root root 1.9M Jul 27 16:30 /var/log/lastlog [root@qls ~]# ll -h /var/log/audit/audit.log -rw-------. 1 root root 1.5M Jul 27 18:01 /var/log/audit/audit.log [root@qls ~]# find /var/log/ -type f -size +2M -ls 67113676 5704 -rw-r--r-- 1 root root 5840292 Jul 27 17:56 /var/log/test.log [root@qls ~]# find /var/log/ -type f -size 6M -ls 67113676 5704 -rw-r--r-- 1 root root 5840292 Jul 27 17:56 /var/log/test.log [root@qls ~]# find /var/log/ -type f -size +2M -size -7M /var/log/test.log [root@qls ~]# dd </dev/zero >/var/log/123.log bs=100M count=13 13+0 records in 13+0 records out 1363148800 bytes (1.4 GB) copied, 28.178 s, 48.4 MB/s [root@qls ~]# ll /var/log/123.log -rw-r--r-- 1 root root 1363148800 Jul 27 18:17 /var/log/123.log [root@qls ~]# ll /var/log/123.log -h -rw-r--r-- 1 root root 1.3G Jul 27 18:17 /var/log/123.log [root@qls ~]# find /var/log/ -type f -size +1G -ls 67588811 1331200 -rw-r--r-- 1 root root 1363148800 Jul 27 18:17 /var/log/123.log [root@qls ~]# find /var/log/ -type f -size 2G -ls 67588811 1331200 -rw-r--r-- 1 root root 1363148800 Jul 27 18:17 /var/log/123.log |
根据文件的时间查找
选项:
-mtime #最后修改时间
+n #多少天以前
-n #多少天以内
n #具体某一天
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
[root@qls ~]# for i in {01..27};do date -s 2020/07/$i && touch data/file-${i}.txt;done [root@qls ~]# [root@qls ~]# ntpdate ntp.aliyun.com [root@qls ~]# ll data/ total 0 -rw-r--r-- 1 root root 0 Jul 10 00:00 file-10.txt -rw-r--r-- 1 root root 0 Jul 11 00:00 file-11.txt -rw-r--r-- 1 root root 0 Jul 12 00:00 file-12.txt -rw-r--r-- 1 root root 0 Jul 13 00:00 file-13.txt [root@qls ~]# [root@qls ~]# find /root -type f -mtime +7 #查找7天以前的 /root/.bash_logout /root/.cshrc /root/.tcshrc /root/.lesshst [root@qls ~]# find /root -type f -mtime 7 #查找第七天 /root/data/file-20.txt [root@qls ~]# find /root -type f -mtime -7 #查找七天以内 /root/.bash_profile /root/.bash_history /root/.viminfo [root@qls ~]# find /root -type f -mtime +3 |xargs rm -f [root@qls ~]# find /var/log/ -type f -size +1G -mtime -1 | xargs rm -f |
根据文件的权限查找
选项:
-perm #根据权限查找
-421 #包含这些权限
421 #精确匹配
/421 #包含其中的某个权限 三个身份中满足一个则匹配
! #取反
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
[root@qls ~]# find /root -type f /root/.bash_history /root/.viminfo /root/data/file-24.txt [root@qls ~]# find /root ! -type f #排除普通的文件的其他所有类型文件 /root /root/.pki /root/.pki/nssdb /root/data #精确匹配 [root@qls ~]# chmod 633 data/file-24.txt [root@qls ~]# ll data/ total 0 -rw--wx-wx 1 root root 0 Jul 24 00:00 file-24.txt -rw-r--r-- 1 root root 0 Jul 25 00:00 file-25.txt [root@qls ~]# find data/ -type f -perm 644 data/file-25.txt [root@qls ~]# find data/ -type f -perm 633 data/file-24.txt #满足查找最低权限 每个身份都有满足 [root@qls ~]# find data/ -type f -perm -644 data/file-26.txt data/file-27.txt [root@qls ~]# ll data/ total 0 -rw--wx-wx 1 root root 0 Jul 24 00:00 file-24.txt -r--r--r-- 1 root root 0 Jul 25 00:00 file-25.txt [root@qls ~]# chmod 666 data/file-25.txt [root@qls ~]# find data/ -type f -perm -644 data/file-25.txt [root@qls ~]# chmod 744 data/file-26.txt [root@qls ~]# find data/ -type f -perm -644 data/file-25.txt #满足一个身份的某个权限就可以 [root@qls ~]# ll data/ [root@qls ~]# find data/ -type f -perm /644 [root@qls ~]# chmod 333 data/file-24.txt [root@qls ~]# find data/ -type f -perm /644 [root@qls ~]# find data/ -type f -perm /444 [root@qls ~]# find / -perm -4000 -ls 201418870 24 -rws--x--x 1 root root 24048 Oct 31 2018 /usr/bin/chfn 201418873 24 -rws--x--x 1 root root 23960 Oct 31 2018 /usr/bin/chsh [root@qls ~]# find / -perm -2000 -ls 201368725 16 -r-xr-sr-x 1 root tty 15344 Jun 10 2014 /usr/bin/wall [root@qls ~]# find / -perm -1000 -ls 67108936 4 drwxrwxrwt 16 root root 4096 Jul 27 2020 /tmp |
根据文件的属主属组查找
选项:
-user #根据属主
-group #根据属组
-nouser #没有属主
-nogroup #没有属组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#查找属主为ops01 [root@qls ~]# find / -user ops01 -ls 201696842 0 -rw-rw---- 1 ops01 mail 0 Jul 24 09:08 /var/spool/mail/ops01 #查找属主为ops01并且 属组为ops01 [root@qls ~]# find / -user ops01 -group ops01 -ls 67588770 0 drwx------ 2 ops01 ops01 83 Jul 24 09:10 /home/ops01 # -a 并且 [root@qls ~]# find / -user ops01 -a -group ops01 -ls 67588770 0 drwx------ 2 ops01 ops01 83 Jul 24 09:10 /home/ops01 [root@qls ~]# find / -user ops01 -a -group ops_group -ls 67495942 0 --wx-wx-wx 1 ops01 ops_group 0 Jul 24 00:00 /root/data/file-24.txt 820161 4 -rw-rw-r-- 1 ops01 ops_group 12 Jul 24 09:16 /data/ops01.log #括号前后要有空格 也需要使用转义字符进行转义 -o 或者 [root@qls ~]# find / \( -user ops01 -o -group ops_group \) -ls 67495942 0 --wx-wx-wx 1 ops01 ops_group 0 Jul 24 00:00 /root/data/file-24.txt #没有属主 [root@qls ~]# find / -nouser -ls 201729427 0 -rw-rw---- 1 666 mail 0 Jul 21 16:47 /var/spool/mail/user02 201685422 0 -rw-rw---- 1 6666 mail 0 Jul 21 16:40 /var/spool/mail/user123 #没有属组的 [root@qls ~]# find / -nogroup -ls 134355712 0 -rw------- 1 root 1013 0 Jul 22 11:11 /var/db/sudo/lectured/qiudao #没有属主也没有属组的 [root@qls ~]# userdel ops02 [root@qls ~]# ll data/file-26.txt 67255910 0 -rwxr--r-- 1 1045 1047 0 Jul 26 00:00 /root/data/file-26.txt 134356976 0 drwx------ 2 1045 1047 99 Jul 24 09:16 /home/ops02 [root@qls ~]# find / \( -nouser -o -nogroup \) -ls 67255910 0 -rwxr--r-- 1 1045 1047 0 Jul 26 00:00 /root/data/file-26.txt 134355712 0 -rw------- 1 root 1013 0 Jul 22 11:11 /var/db/sudo/lectured/qiudao 基础很重要 |
根据文件的目录层级查找
选项:
-maxdepth #最大的目录层级 tree -L
1 2 3 4 5 6 |
[root@qls ~]# find / -maxdepth 2 \( -nouser -o -nogroup \) -ls 134356976 0 drwx------ 2 1045 1047 99 Jul 24 09:16 /home/ops02 818815 0 -rw-rw-r-- 1 1045 1047 0 Jul 24 09:11 /data/ops02.txt [root@qls ~]# find / -maxdepth 3 \( -nouser -o -nogroup \) -ls 67255910 0 -rwxr--r-- 1 1045 1047 0 Jul 26 00:00 /root/data/file-26.txt |
Find的执行的动作
-print #默认的动作 打印你所查找出来的文件的名称
-ls #把查找出来的文件以长格形式显示出来文件的详细信息
-delete #把查找出来的文件进行删除 只能删除空目录
-exec #后面加自定义的shell命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
[root@qls ~]# find / -maxdepth 2 \( -nouser -o -nogroup \) /home/ops02 /data/ops02.txt /data/ops02.log /data/test [root@qls ~]# find / -maxdepth 2 \( -nouser -o -nogroup \) -print /home/ops02 /data/ops02.txt /data/ops02.log /data/test [root@qls ~]# find / -maxdepth 2 \( -nouser -o -nogroup \) -ls 134356976 0 drwx------ 2 1045 1047 99 Jul 24 09:16 /home/ops02 818815 0 -rw-rw-r-- 1 1045 1047 0 Jul 24 09:11 /data/ops02.txt 820162 4 -rw-rw-r-- 1 1045 ops_group 6 Jul 24 09:15 /data/ops02.log 818814 0 drwxrwsr-x 2 1045 ops_group 6 Jul 24 09:21 /data/test [root@qls ~]# find /opt/ -type d -name "opt" /opt/ [root@qls ~]# ll /opt/ total 0 [root@qls ~]# cp /etc/hosts /opt/ [root@qls ~]# find /opt/ -type d -name "opt" -delete find: cannot delete ‘/opt/’: Directory not empty [root@qls ~]# ll /opt/ total 4 -rw-r--r-- 1 root root 158 Jul 27 12:11 hosts [root@qls ~]# find /opt/ -type f -name "hosts" -delete [root@qls ~]# ll /opt/ total 0 [root@qls ~]# find /opt/ -type d -name "opt" -delete [root@qls ~]# find /root/data/ -type f /root/data/file-24.txt /root/data/file-25.txt /root/data/file-26.txt /root/data/file-27.txt [root@qls ~]# find /root/data/ -type f -delete [root@qls ~]# ll data/ total 0 #把某个目录下七天以前的数据删除 [root@qls ~]# find /backup -type f -mtime +7 -delete [root@qls ~]# find /var/log/ -type f -name "*.log" -exec cp {} /opt/ \; [root@qls ~]# ll /opt/ total 9076 -rw-r--r-- 1 root root 0 Jul 27 12:17 access.log -rw------- 1 root root 26591 Jul 27 12:17 anaconda.log -rw------- 1 root root 1485709 Jul 27 12:17 audit.log |
Find命令跟xargs的配合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
[root@qls ~]# find /var/log/ -type f -name "*.log" | xargs cp -t /opt/a [root@qls ~]# ll /opt/a total 9076 -rw-r--r-- 1 root root 0 Jul 27 12:22 access.log -rw------- 1 root root 26591 Jul 27 12:22 anaconda.log [root@qls ~]# find /var/log/ -type f -name "*.log" | xargs -I {} cp {} /opt/b [root@qls ~]# ll /opt/b total 9076 -rw-r--r-- 1 root root 0 Jul 27 12:23 access.log -rw------- 1 root root 26591 Jul 27 12:23 anaconda.log [root@qls ~]# cp `find /var/log/ -type f -name "*.log"` /opt/c [root@qls ~]# ll /opt/c total 9076 -rw-r--r-- 1 root root 0 Jul 27 12:24 access.log [root@qls ~]# \cp $(find /var/log/ -type f -name "*.log") /opt/c $() == `` #优先执行里面的命令 把命令执行的结果交给外面的命令 [root@qls ~]# find /var/log/ -type f -name "*.log" |xargs ls -l -rw-------. 1 root root 26591 Jul 6 02:17 /var/log/anaconda/anaconda.log -rw-------. 1 root root 4017 Jul 6 02:17 /var/log/anaconda/ifcfg.log -rw-------. 1 root root 1552657 Jul 6 02:17 /var/log/anaconda/journal.log -rw-------. 1 root root 0 Jul 6 02:17 /var/log/anaconda/ks-script-3PCKae.log -rw-------. 1 root root 110647 Jul 6 02:17 /var/log/anaconda/packaging.log -rw-------. 1 root root 29721 Jul 6 02:17 /var/log/anaconda/program.log [root@qls ~]# find /opt/a -type f -name "*.log" |xargs rm -f [root@qls ~]# ll /opt/a total 0 |