【NAME】
find - search for files in a directory hierarchy
搜索目录中的文件层次结构
功能:
实时查找工具,通过遍历指定起始路径下文件系统层级结构完成文件查找;
工作特性:
查找速度略慢;
精确查找;
实时查找;
【SYNOPSIS】
【OPTIONS】
[root@zck ~]# find /etc/ -name "passwd"
/etc/passwd
/etc/pam.d/passwd
[root@zck ~]# find /etc -name "passwd"
/etc/passwd
/etc/pam.d/passwd
[root@zck ~]# mkdir /etc/test
[root@zck ~]# touch /etc/test/Passwd
[root@zck ~]# touch /etc/test/MPASSWD.txt
[root@zck ~]# find /etc -iname "passwd" #-i,忽略大小写
/etc/passwd
/etc/pam.d/passwd
/etc/test/Passwd
[root@zck ~]# find /etc -name "passwd"
/etc/passwd
/etc/pam.d/passwd
[root@zck ~]# find /etc -name "passwd*"
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
[root@zck ~]# find /etc -name "*passwd"
/etc/passwd
/etc/security/opasswd
/etc/pam.d/passwd
[root@zck ~]# touch /etc/test/npasswd
[root@zck ~]# touch /etc/test/npasswdo
[root@zck ~]# find /etc -name "*passwd"
/etc/passwd
/etc/security/opasswd
/etc/pam.d/passwd
/etc/test/npasswd
[root@zck ~]# find /etc -name "passwd?"
/etc/passwd-
[root@zck ~]# find /etc -name "?passwd"
/etc/security/opasswd
/etc/test/npasswd
[root@zck ~]# touch /etc/test/passwdx
[root@zck ~]# find /etc -name "passwd?"
/etc/passwd-
/etc/test/passwdx
[root@zck ~]# find /etc -name "passwd[[:alnum:]]" #查找passwd结尾即可是字母又可是数字的字符
/etc/test/passwdx
[root@zck ~]# cd /etc/test/
[root@zck test]# find -name "passwd*" -o -iname "mpasswd.txt" #不加路径,默认是查找当前目录下文件
./MPASSWD.txt
./passwdx
[root@zck test]# find -name "passwd*" -o -name "mpasswd.txt" #-o:表示或,两个参数只要符合一个即可
./passwdx
[root@zck test]# find -name "passwd*" -o -iname "mpasswd*"
./MPASSWD.txt
./passwdx
[root@zck ~]# find /tmp -user "moosefs" #查找/etc目录下,所属用户是"mosefs"的文件
find: ‘moosefs’ is not the name of a known user #提示没有些用户的该用
[root@zck ~]# find /tmp -user user1 -ls #查找/tmp目录下,所属用户是"user1"的文件
206262276 0 -rw-rw-r-- 1 user1 user1 0 Aug 22 11:03 /tmp/test1.txt
206262277 0 -rw-rw-r-- 1 user1 user1 0 Aug 22 11:03 /tmp/test2.txt
[root@zck ~]# find /tmp -group user3 -ls #查找/tmp目录下,所属组是"user3"的文件
206262277 0 -rw-rw-r-- 1 user3 user3 0 Aug 22 11:03 /tmp/test2.txt
[root@zck ~]# find /tmp -nouser -ls #查找/tmp目录下,没有所属用户的文件
206262276 0 -rw-rw-r-- 1 1006 root 0 Aug 22 11:03 /tmp/test1.txt
[root@zck ~]# find /tmp -nogroup -ls #查找/tmp目录下,没有所属组的文件
206262277 0 -rw-rw-r-- 1 root 1007 0 Aug 22 11:03 /tmp/test2.txt
[root@zck ~]# find /tmp -uid 1006 -ls #查找/tmp目录下,uid是1006的文件
206262276 0 -rw-rw-r-- 1 1006 root 0 Aug 22 11:03 /tmp/test1.txt
[root@zck ~]# find /tmp -gid 1007 -ls #查找/tmp目录下,gid是1007的文件
206262277 0 -rw-rw-r-- 1 root 1007 0 Aug 22 11:03 /tmp/test2.txt
[root@zck ~]# find /tmp -uid +500 -ls #查找/tmp目录下,uid大于500的文件 138156780 0 drwx------ 2 roo roo 6 Aug 11 15:37 /tmp/.esd-1000 206262276 0 -rw-rw-r-- 1 1006 root 0 Aug 22 11:03 /tmp/test1.txt
[root@zck ~]# find /dev -type b -ls
10674 0 brw-rw---- 1 root cdrom 11, 0 Aug 12 14:39 /dev/sr0
10661 0 brw-rw---- 1 root disk 8, 3 Aug 12 14:39 /dev/sda3
10660 0 brw-rw---- 1 root disk 8, 2 Aug 12 14:39 /dev/sda2
10659 0 brw-rw---- 1 root disk 8, 1 Aug 12 14:39 /dev/sda1
10655 0 brw-rw---- 1 root disk 8, 0 Aug 12 14:39 /dev/sda
[root@zck ~]# find /dev -type c -ls 19289 0 crw------- 1 root root 10, 57 Aug 12 14:39 /dev/vsock 19145 0 crw-rw---- 1 root tty 7, 134 Aug 12 14:39 /dev/vcsa6
- [root@zck ~]# find /tmp -size 180k #查找/tmp目录下大小是180k的文件
/tmp/messages
[root@zck ~]# find /tmp -size -180k
#查找/tmp目录下小于180k的文件/tmp
/tmp/.font-unix
/tmp/.X11-unix
/tmp/.X11-unix/X0
/tmp/.Test-unix
/tmp/.ICE-unix
/tmp/.ICE-unix/2410
/tmp/.XIM-unix
/tmp/.esd-0
/tmp/.esd-1000
/tmp/test
/tmp/test/a
/tmp/test/c
/tmp/test/d
/tmp/test/f
/tmp/test/g
/tmp/test/b.danger
/tmp/test/e.danger
/tmp/test1.txt
/tmp/test2.txt
/tmp/.X0-lock
[root@zck ~]# find /tmp -size +180k
#查找/tmp目录下大于180k的文件/tmp/messages.2
[root@zck ~]# find /tmp -atime +7 -ls #查找/tmp目录下7天之前访问过的文件
[root@zck ~]# find /tmp -mtime -1 -ls
#查找/tmp目录下1天之内修改过的文件[root@zck ~]# find /tmp -amin +60
#查找/tmp目录下60分钟之前修改过的文件[root@zck ~]# find /tmp -mmin -60
#查找/tmp目录下60分钟之内修改过的文件
mode精确权限匹配:
[root@zck ~]# mkdir /tmp/test
[root@zck ~]# cd /tmp/test/
[root@zck test]# touch a b c d e f g
[root@zck test]# chmod 640 a
[root@zck test]# chmod 666 b
[root@zck test]# chmod 440 c
[root@zck test]# chmod 775 d
[root@zck test]# chmod 777 e
[root@zck test]# ll
total 0
-rw-r-----. 1 root root 0 Aug 22 16:23 a
-rw-rw-rw-. 1 root root 0 Aug 22 16:23 b
-r--r-----. 1 root root 0 Aug 22 16:23 c
-rwxrwxr-x. 1 root root 0 Aug 22 16:23 d
-rwxrwxrwx. 1 root root 0 Aug 22 16:23 e
-rw-r--r--. 1 root root 0 Aug 22 16:23 f
-rw-r--r--. 1 root root 0 Aug 22 16:23 g
[root@zck test]# find ./ -perm 644 -ls
205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./f
205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./g
[root@zck test]# find ./ -perm 666 -ls
205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 ./b
/mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件即满足;
[root@zck test]# find ./ -perm /666 -ls
205118792 0 drwxr-xr-x 2 root root 62 Aug 22 16:23 ./
205118796 0 -rw-r----- 1 root root 0 Aug 22 16:23 ./a
205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 ./b
205118798 0 -r--r----- 1 root root 0 Aug 22 16:23 ./c
205118799 0 -rwxrwxr-x 1 root root 0 Aug 22 16:23 ./d
205118802 0 -rwxrwxrwx 1 root root 0 Aug 22 16:23 ./e
205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./f
205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./g
[root@zck test]# find ./ -perm /222 -ls
205118792 0 drwxr-xr-x 2 root root 62 Aug 22 16:23 ./
205118796 0 -rw-r----- 1 root root 0 Aug 22 16:23 ./a
205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 ./b
205118799 0 -rwxrwxr-x 1 root root 0 Aug 22 16:23 ./d
205118802 0 -rwxrwxrwx 1 root root 0 Aug 22 16:23 ./e
205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./f
205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./g
-mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件即满足;
[root@zck test]# find ./ -perm -222 -ls #查找当前目录所有用户才有写权限的文件
205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 ./b
205118802 0 -rwxrwxrwx 1 root root 0 Aug 22 16:23 ./e
[root@zck test]# find ./ -not -perm -222 -ls
#查找当前目录至少有一个用户有写权限的文件205118792 0 drwxr-xr-x 2 root root 62 Aug 22 16:23 ./
205118796 0 -rw-r----- 1 root root 0 Aug 22 16:23 ./a
205118798 0 -r--r----- 1 root root 0 Aug 22 16:23 ./c
205118799 0 -rwxrwxr-x 1 root root 0 Aug 22 16:23 ./d
205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./f
205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 ./g
[root@zck test]# find /tmp -empty -ls
135772013 0 drwxrwxrwt 2 root root 6 Jul 27 17:31 /tmp/.font-unix
68273776 0 drwxrwxrwt 2 root root 6 Jul 27 17:31 /tmp/.Test-unix
202211348 0 drwxrwxrwt 2 root root 6 Jul 27 17:31 /tmp/.XIM-unix
205130530 0 drwx------ 2 root root 6 Aug 11 15:46 /tmp/.esd-0
138156780 0 drwx------ 2 roo roo 6 Aug 11 15:37 /tmp/.esd-1000
206262276 0 -rw-rw-r-- 1 1006 root 0 Aug 22 11:03 /tmp/test1.txt
206262277 0 -rw-rw-r-- 1 root 1007 0 Aug 22 11:03 /tmp/test2.txt
205118796 0 -rw-r----- 1 root root 0 Aug 22 16:23 /tmp/test/a
205118797 0 -rw-rw-rw- 1 root root 0 Aug 22 16:23 /tmp/test/b
205118798 0 -r--r----- 1 root root 0 Aug 22 16:23 /tmp/test/c
205118799 0 -rwxrwxr-x 1 root root 0 Aug 22 16:23 /tmp/test/d
205118802 0 -rwxrwxrwx 1 root root 0 Aug 22 16:23 /tmp/test/e
205118804 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 /tmp/test/f
205118807 0 -rw-r--r-- 1 root root 0 Aug 22 16:23 /tmp/test/g
[root@CentOS7-171 ~]# find /tmp -not -user root -ls
139050703 0 drwxr-xr-x 2 tomcat tomcat 18 Feb 5 22:40 /tmp/hsperfdata_tomcat
139052243 32 -rw------- 1 tomcat tomcat 32768 Mar 10 15:16 /tmp/hsperfdata_tomcat/63194
136064490 0 drwxr----- 3 3003 3003 74 Mar 1 14:34 /tmp/skel
136064504 4 -rw-r----- 1 3003 3003 18 Mar 1 14:34 /tmp/skel/.bash_logout
136064508 4 -rw-r----- 1 3003 3003 193 Mar 1 14:34 /tmp/skel/.bash_profile
136064512 4 -rw-r----- 1 3003 3003 231 Mar 1 14:34 /tmp/skel/.bashrc
205899564 0 drwxr----- 4 3003 3003 37 Mar 1 14:34 /tmp/skel/.mozilla
1028311 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/extensions
68961524 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/plugins
1028313 4 -rw-r----- 1 archlinux mygrp 511 Mar 1 15:09 /tmp/inittab
[root@CentOS7-171 ~]# find -not -name "*fstab*" -ls
[root@CentOS7-171 ~]# find /tmp -not -user root -a -not -name "*fstab*" -ls
139050703 0 drwxr-xr-x 2 tomcat tomcat 18 Feb 5 22:40 /tmp/hsperfdata_tomcat
139052243 32 -rw------- 1 tomcat tomcat 32768 Mar 10 15:20 /tmp/hsperfdata_tomcat/63194
136064490 0 drwxr----- 3 3003 3003 74 Mar 1 14:34 /tmp/skel
136064504 4 -rw-r----- 1 3003 3003 18 Mar 1 14:34 /tmp/skel/.bash_logout
136064508 4 -rw-r----- 1 3003 3003 193 Mar 1 14:34 /tmp/skel/.bash_profile
136064512 4 -rw-r----- 1 3003 3003 231 Mar 1 14:34 /tmp/skel/.bashrc
205899564 0 drwxr----- 4 3003 3003 37 Mar 1 14:34 /tmp/skel/.mozilla
1028311 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/extensions
68961524 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/plugins
1028313 4 -rw-r----- 1 archlinux mygrp 511 Mar 1 15:09 /tmp/inittab
[root@CentOS7-171 ~]# find /tmp -not \( -user root -o -name "*fstab*" \) -ls
139050703 0 drwxr-xr-x 2 tomcat tomcat 18 Feb 5 22:40 /tmp/hsperfdata_tomcat
139052243 32 -rw------- 1 tomcat tomcat 32768 Mar 10 15:21 /tmp/hsperfdata_tomcat/63194
136064490 0 drwxr----- 3 3003 3003 74 Mar 1 14:34 /tmp/skel
136064504 4 -rw-r----- 1 3003 3003 18 Mar 1 14:34 /tmp/skel/.bash_logout
136064508 4 -rw-r----- 1 3003 3003 193 Mar 1 14:34 /tmp/skel/.bash_profile
136064512 4 -rw-r----- 1 3003 3003 231 Mar 1 14:34 /tmp/skel/.bashrc
205899564 0 drwxr----- 4 3003 3003 37 Mar 1 14:34 /tmp/skel/.mozilla
1028311 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/extensions
68961524 0 drwxr----- 2 3003 3003 6 Mar 1 14:34 /tmp/skel/.mozilla/plugins
1028313 4 -rw-r----- 1 archlinux mygrp 511 Mar 1 15:09 /tmp/inittab
[root@CentOS7-171 test]# find / \( -nouser -o -nogroup \) -atime -7 -ls
[root@CentOS7-171 test]# find /etc/ -size +1M -type f -exec ls -lh {} \;
-r--r--r--. 1 root root 6.7M Aug 31 2016 /etc/udev/hwdb.bin
-rw-r--r--. 1 root root 1.4M Nov 22 2015 /etc/gconf/schemas/ekiga.schemas
-rw-r--r--. 1 root root 3.7M Nov 21 2015 /etc/selinux/targeted/policy/policy.29
-rw-r--r--. 1 root root 1.4M Mar 6 2015 /etc/brltty/zh-tw.ctb
[root@CentOS7-171 test]# find /etc/ -not -perm /222 -type f -ls
[root@CentOS7-171 test]# find /etc -not -perm -111 -type f -ls
[root@CentOS7-171 test]# find /etc/init.d/ -perm -113 -type f -ls
原文地址:http://zhucke.blog.51cto.com/180229/1906808