文件查找find

find -name 名称查找
[root@rstx-53 test]# ls
eth0  ETH0  file1  file2  file3  file4  file5
[root@rstx-53 test]# find ./ -name 'eth0'
./eth0
find -iname 名称查找不区分大小写
[root@rstx-53 test]# find ./ -iname 'eth0'
./eth0
./ETH0
find -size +5M 根据文件大小查找文件
[root@rstx-53 test]# find /etc/ -size +5M
/etc/udev/hwdb.bin
/etc/ansible/roles/ca_ssl/files/cfssl-certinfo_linux-amd64
[root@rstx-53 test]# find /etc/ -size +2014k
/etc/selinux/targeted/policy/policy.31
/etc/selinux/targeted/active/policy.kern
[root@rstx-53 test]# find /etc/ -size +5M|xargs ls -lh
-rw-r--r-- 1 root root 6.3M Dec 29 11:25 /etc/ansible/roles/ca_ssl/files/cfssl-certinfo_linux-amd64
-rw-r--r-- 1 root root 9.9M Dec 29 11:24 /etc/ansible/roles/ca_ssl/files/cfssl_linux-amd64
find -type f/d 根据文件类型查找文件
[root@rstx-53 test]# find /etc/ -type f -size +5M|xargs ls -lh
-rw-r--r-- 1 root root 6.3M Dec 29 11:25 /etc/ansible/roles/ca_ssl/files/cfssl-certinfo_linux-amd64
-rw-r--r-- 1 root root 9.9M Dec 29 11:24 /etc/ansible/roles/ca_ssl/files/cfssl_linux-amd64
-r--r--r-- 1 root root 8.5M Feb 24 15:48 /etc/udev/hwdb.bin

[root@rstx-53 test]# find /etc/ -type l |xargs ls -lh
lrwxrwxrwx  1 root root 77 Feb 24 15:47 /etc/alternatives/alt-java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64/jre/bin/alt-java
lrwxrwxrwx  1 root root 81 Feb 24 15:47 /etc/alternatives/alt-java.1.gz -> /usr/share/man/man1/alt-java-java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64.1.gz
lrwxrwxrwx  1 root root 73 Feb 24 15:47 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64/jre/bin/java
lrwxrwxrwx  1 root root 77 Feb 24 15:47 /etc/alternatives/java.1.gz -> /usr/share/man/man1/java-java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64.1.gz
find -mtime
find mtime 
7   第七天创建的文件
+7  创建时间大于七天的文件或者目录
-7  创建时间小于七天的文件或者目录
[root@rstx-53 test]# find /var -mtime 7|xargs ls -l 
-rw-rw-r-- 1 nginx root    9004 Feb 27 03:15 /var/log/nginx/access.log-20210227.gz
-rw-r--r-- 1 root  root 1120352 Feb 26 23:50 /var/log/sa/sa26
-rw-r--r-- 1 root  root  829315 Feb 26 23:53 /var/log/sa/sar26
[root@rstx-53 test]# find /var -mtime +7|xargs ls -l |head 
-rw-r--r--.   1 root root    64 Sep 28 17:13 /var/lib/yum/yumdb/p/000cbf4b21d5dc2523c500f17ae484dcf5b0a605-python36-chardet-3.0.4-1.el7-noarch/checksum_data
-rw-r--r--.  17 root root     6 Sep 28 17:13 /var/lib/yum/yumdb/p/000cbf4b21d5dc2523c500f17ae484dcf5b0a605-python36-chardet-3.0.4-1.el7-noarch/checksum_type
-rw-r--r--.  17 root root    25 Sep 28 17:13 /var/lib/yum/yumdb/p/000cbf4b21d5dc2523c500f17ae484dcf5b0a605-python36-chardet-3.0.4-1.el7-noarch/command_line
[root@rstx-53 test]# find /var -mtime -7|xargs ls -l |head 
-rw-r--r--   1 root        root           16384 Mar  6 03:34 /var/cache/man/cs/index.db
-rw-r--r--   1 root        root           16384 Mar  6 03:34 /var/cache/man/da/index.db
[root@rstx-53 test]# find ./ -mtime +7 -delete   #查找大于七天的文件并删除
[root@rstx-53 test]# ls -l
-rw-r--r-- 1 root root 0 Mar  6 10:59 eth0
-rw-r--r-- 1 root root 0 Mar  6 10:59 ETH0
find / maxdepth 1 -user -group maxdepth目录层级 user用户 group 用户组
[root@rstx-53 test]# find / -maxdepth 1 -user root -group  root  -mtime +7 
/var
/usr
/bin
find -a 并且的关系
[root@rstx-53 test]# find / -maxdepth 1 -user root -a -group  root  -mtime +7 
/var
/usr
/bin
find -o 或者的关系
[root@rstx-53 test]# find / -maxdepth 1 -user root -a -group  root  -mtime +7 
/var
/usr
/bin
find -perm 根据权限查找
[root@rstx-53 test]# find ./ -perm 644 
./file1
./file2
./file3
find + sed 统计文件大小
[root@rstx-53 test]# find /opt -type f -size +500M|sed -r 's#(.*)#du -sh \1#g'|bash 
643M	/opt/src/harbor-offline-installer-v1.10.1.tgz
531M	/opt/src/harbor-offline-installer-v2.1.0.tgz
647M	/opt/harbor-v1.10.1/harbor.v1.10.1.tar.gz
533M	/opt/harbor-v1.10.1/harbor/harbor.v2.1.0.tar.gz
533M	/opt/harbor-v2.1.0/harbor.v2.1.0.tar.gz
find -exec ; 联合执行 ;不可缺少
[root@rstx-53 packages]# find /etc/ -type f  -mtime -1 -exec ls {} \;
/etc/yum.conf
/etc/yum.repos.d/zabbix.repo
/etc/yum.repos.d/docker-ce.repo
/etc/yum.repos.d/pgdg-redhat-all.repo.gz
/etc/openvpn/server/openvpn-status.log
/etc/openvpn/server/ipp.txt
find -delete 查找文件并删除
root@rongbiz:~/test# touch 1.txt
root@rongbiz:~/test# ls
1.txt  2019.mp4  2019.ts  2019nianhui.avi  2019nianhui.mp4
root@rongbiz:~/test# find ./ -name '1.txt' -delete
root@rongbiz:~/test# ls
2019.mp4  2019.ts  2019nianhui.avi  2019nianhui.mp4
posted @ 2021-03-06 14:17  老夫聊发少年狂88  阅读(68)  评论(0编辑  收藏  举报