linux命令find用法
文件查找:
在文件系统上查找符合条件的文件;
文件查找:locate, find
非实时查找(数据库查找):locate
实时查找:find
yum provides */locate
[root@MyServer ~]# locate aa.txt /root/aa.txt
locate:
依赖于事先构建的索引;索引的构建是在系统较为空闲时自动进行(周期性任务);手动更新数据库(updatedb);
索引构建过程需要遍历整个根文件系统,极消耗资源;
工作特点:
查找速度快;
模糊查找;
非实时查找;
locate KEYWORD
重点是find命令
find:
实时查找工具,通过遍历指定路径下的文件系统完成文件查找;
工作特点:
查找速度略慢;
精确查找;
实时查找;
语法:
find [OPTION]... [查找路径] [查找条件] [处理动作]
查找路径:指定具体目标路径;默认为当前目录;
[root@MyServer ~]# find passwd find: `passwd‘: No such file or directory [root@MyServer ~]# find -name passwd
查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件;
处理动作:对符合条件的文件做什么操作;默认输出至屏幕;
查找条件:
根据文件名查找:
-name "文件名称":支持使用glob
*, ?, [], [^]
[root@MyServer ~]# find / -name "hosts" /tmp/hosts /etc/hosts [root@MyServer ~]# find / -name hosts /tmp/hosts /etc/hosts
查找/etc 下面 passwd的文件
[root@MyServer ~]# find /etc/ -name passwd /etc/pam.d/passwd /etc/passwd [root@MyServer ~]# find /etc/ -name passwd* /etc/passwd- /etc/pam.d/passwd /etc/passwd [root@MyServer ~]# find /etc/ -name *passwd* /etc/passwd- /etc/pam.d/passwd /etc/passwd /etc/security/opasswd
-iname "文件名称":不区分字母大小写
[root@MyServer ~]# find /etc/ -iname *passwd* /etc/passwd- /etc/pam.d/passwd /etc/passwd /etc/security/opasswd
-regex "PATTERN":以PATTERN匹配整个文件路径字符串,而不仅仅是文件名称;
根据属主、属组查找:
-user USERNAME:查找属主为指定用户的文件;
group GRPNAME: 查找属组为指定组的文件;
[root@MyServer ~]# find / hosts -user user1 -ls find: `/proc/20130/task/20130/fd/5‘: No such file or directory find: `/proc/20130/task/20130/fdinfo/5‘: No such file or directory find: `/proc/20130/fd/5‘: No such file or directory find: `/proc/20130/fdinfo/5‘: No such file or directory 259659 4 -rw-r--r-- 1 user1 user1 158 Jul 17 13:49 /tmp/hosts 390081 0 -rw-rw---- 1 user1 mail 0 Jul 16 20:05 /var/spool/mail/user1 32065 4 drwx------ 2 user1 user1 4096 Jul 18 20:38 /home/user1 32070 4 -rw-rw-r-- 1 user1 user1 5 Jul 18 20:38 /home/user1/a.txt 32066 4 -rw-r--r-- 1 user1 user1 124 Sep 26 2014 /home/user1/.bashrc 32067 4 -rw-r--r-- 1 user1 user1 176 Sep 26 2014 /home/user1/.bash_profile 32069 4 -rw------- 1 user1 user1 60 Jul 18 20:38 /home/user1/.bash_history 32068 4 -rw-r--r-- 1 user1 user1 18 Sep 26 2014 /home/user1/.bash_logout [root@MyServer ~]# find / -user user1 -ls find: `/proc/20131/task/20131/fd/5‘: No such file or directory find: `/proc/20131/task/20131/fdinfo/5‘: No such file or directory find: `/proc/20131/fd/5‘: No such file or directory find: `/proc/20131/fdinfo/5‘: No such file or directory 259659 4 -rw-r--r-- 1 user1 user1 158 Jul 17 13:49 /tmp/hosts 390081 0 -rw-rw---- 1 user1 mail 0 Jul 16 20:05 /var/spool/mail/user1 32065 4 drwx------ 2 user1 user1 4096 Jul 18 20:38 /home/user1 32070 4 -rw-rw-r-- 1 user1 user1 5 Jul 18 20:38 /home/user1/a.txt 32066 4 -rw-r--r-- 1 user1 user1 124 Sep 26 2014 /home/user1/.bashrc 32067 4 -rw-r--r-- 1 user1 user1 176 Sep 26 2014 /home/user1/.bash_profile 32069 4 -rw------- 1 user1 user1 60 Jul 18 20:38 /home/user1/.bash_history 32068 4 -rw-r--r-- 1 user1 user1 18 Sep 26 2014 /home/user1/.bash_logout [root@MyServer ~]# find / -group user1 -ls find: `/proc/20169/task/20169/fd/5‘: No such file or directory find: `/proc/20169/task/20169/fdinfo/5‘: No such file or directory find: `/proc/20169/fd/5‘: No such file or directory find: `/proc/20169/fdinfo/5‘: No such file or directory 259659 4 -rw-r--r-- 1 user1 user1 158 Jul 17 13:49 /tmp/hosts 32065 4 drwx------ 2 user1 user1 4096 Jul 18 20:38 /home/user1 32070 4 -rw-rw-r-- 1 user1 user1 5 Jul 18 20:38 /home/user1/a.txt 32066 4 -rw-r--r-- 1 user1 user1 124 Sep 26 2014 /home/user1/.bashrc 32067 4 -rw-r--r-- 1 user1 user1 176 Sep 26 2014 /home/user1/.bash_profile 32069 4 -rw------- 1 user1 user1 60 Jul 18 20:38 /home/user1/.bash_history 32068 4 -rw-r--r-- 1 user1 user1 18 Sep 26 2014 /home/user1/.bash_logout
如果用户被删除,之前属于的文件没有宿主,只有uid了
[root@MyServer ~]# ll /home/user1 total 4 -rw-rw-r-- 1 500 500 5 Jul 18 20:38 a.txt [root@MyServer ~]# tail -3 /etc/passwd user1:x:500:500::/home/user1:/bin/bash aaa:x:501:501::/home/aaa:/bin/bash user2:x:502:502::/home/user2:/bin/bash [root@MyServer ~]# userdel user2 [root@MyServer ~]# find / -uid 502 find: `/proc/20431/task/20431/fd/5‘: No such file or directory find: `/proc/20431/task/20431/fdinfo/5‘: No such file or directory find: `/proc/20431/fd/5‘: No such file or directory find: `/proc/20431/fdinfo/5‘: No such file or directory /var/spool/mail/user2 /home/user2 /home/user2/.bashrc /home/user2/.bash_profile /home/user2/.bash_logout
-uid UserID:查找属主为指定的UID号的文件;
-gid GroupID:查找属组为指定的GID号的文件;
[root@MyServer ~]# find / -gid 502 find: `/proc/20520/task/20520/fd/5‘: No such file or directory find: `/proc/20520/task/20520/fdinfo/5‘: No such file or directory find: `/proc/20520/fd/5‘: No such file or directory find: `/proc/20520/fdinfo/5‘: No such file or directory /home/user2 /home/user2/.bashrc /home/user2/.bash_profile /home/user2/.bash_logout
-nouser:查找没有属主的文件;
-nogroup:查找没有属组的文件;
[root@MyServer ~]# find / -nouser find: `/proc/20571/task/20571/fd/5‘: No such file or directory find: `/proc/20571/task/20571/fdinfo/5‘: No such file or directory find: `/proc/20571/fd/5‘: No such file or directory find: `/proc/20571/fdinfo/5‘: No such file or directory /var/spool/mail/user2 /home/user2 /home/user2/.bashrc /home/user2/.bash_profile /home/user2/.bash_logout [root@MyServer ~]# userdel user1 [root@MyServer ~]# ll /home/user1 total 4 -rw-rw-r-- 1 500 500 5 Jul 18 20:38 a.txt [root@MyServer ~]# find / -nouser find: `/proc/20615/task/20615/fd/5‘: No such file or directory find: `/proc/20615/task/20615/fdinfo/5‘: No such file or directory find: `/proc/20615/fd/5‘: No such file or directory find: `/proc/20615/fdinfo/5‘: No such file or directory /tmp/hosts /var/spool/mail/user2 /var/spool/mail/user1 /home/user1 /home/user1/a.txt /home/user1/.bashrc /home/user1/.bash_profile /home/user1/.bash_history /home/user1/.bash_logout /home/user2 /home/user2/.bashrc /home/user2/.bash_profile /home/user2/.bash_logout
根据文件类型查找:
-type TYPE:
f: 普通文件
d: 目录文件
l: 符号链接文件
s:套接字文件
b: 块设备文件
c: 字符设备文件
p: 管道文件
查找/tmp下面文件类型为目录的
[root@MyServer ~]# find /tmp -type d /tmp /tmp/.ICE-unix /tmp/mytest2 /tmp/mytest2/popt.d /tmp/mytest2/yum.repos.d /tmp/mytest2/makedev.d /tmp/mytest2/pam.d /tmp/mytest2/cron.d /tmp/mytest2/sudoers.d /tmp/mytest2/ld.so.conf.d /tmp/mytest2/rsyslog.d /tmp/mytest2/depmod.d /tmp/mytest2/logrotate.d /tmp/mytest2/rwtab.d /tmp/mytest2/dracut.conf.d /tmp/mytest2/rc.d /tmp/mytest2/rc.d/rc0.d /tmp/mytest2/rc.d/rc6.d /tmp/mytest2/rc.d/rc3.d /tmp/mytest2/rc.d/rc2.d /tmp/mytest2/rc.d/rc4.d /tmp/mytest2/rc.d/rc1.d /tmp/mytest2/rc.d/rc5.d /tmp/mytest2/rc.d/init.d /tmp/mytest2/modprobe.d /tmp/mytest2/chkconfig.d /tmp/mytest2/statetab.d /tmp/mytest2/bash_completion.d /tmp/mytest2/xinetd.d /tmp/mytest2/profile.d /tmp/mytest3 /tmp/mytest1 /tmp/mytest1/popt.d /tmp/mytest1/ppp /tmp/mytest1/ppp/peers /tmp/mytest1/pam.d /tmp/mytest1/pm /tmp/mytest1/pm/power.d /tmp/mytest1/pm/config.d /tmp/mytest1/pm/sleep.d /tmp/mytest1/plymouth /tmp/mytest1/postfix /tmp/mytest1/pki /tmp/mytest1/pki/rsyslog /tmp/mytest1/pki/CA /tmp/mytest1/pki/CA/certs /tmp/mytest1/pki/CA/private /tmp/mytest1/pki/CA/newcerts /tmp/mytest1/pki/CA/crl /tmp/mytest1/pki/tls /tmp/mytest1/pki/tls/certs /tmp/mytest1/pki/tls/misc /tmp/mytest1/pki/tls/private /tmp/mytest1/pki/nssdb /tmp/mytest1/pki/rpm-gpg /tmp/mytest1/pki/java /tmp/mytest1/profile.d /tmp/mylinux /tmp/mylinux/lib64 /tmp/mylinux/proc /tmp/mylinux/dev /tmp/mylinux/lib /tmp/mylinux/lib/modules /tmp/mylinux/bin /tmp/mylinux/boot /tmp/mylinux/boot/grub /tmp/mylinux/sys /tmp/mylinux/tmp /tmp/mylinux/etc /tmp/mylinux/etc/sysconfig /tmp/mylinux/etc/sysconfig/network-scripts /tmp/mylinux/etc/rc.d /tmp/mylinux/etc/rc.d/init.d /tmp/mylinux/sbin /tmp/mylinux/var /tmp/mylinux/var/run /tmp/mylinux/var/log /tmp/mylinux/var/lock /tmp/mylinux/usr /tmp/mylinux/usr/local /tmp/mylinux/usr/local/bin /tmp/mylinux/usr/local/sbin
查找/下面类型为套接字的文件
[root@MyServer ~]# find / -type s -ls find: `/proc/20886/task/20886/fd/5‘: No such file or directory find: `/proc/20886/task/20886/fdinfo/5‘: No such file or directory find: `/proc/20886/fd/5‘: No such file or directory find: `/proc/20886/fdinfo/5‘: No such file or directory 7729 0 srw-rw-rw- 1 root root 0 Jul 18 13:22 /dev/log 389533 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/error 389524 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/proxymap 389521 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/trace 389538 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/virtual 389540 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/anvil 389520 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/defer 389539 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/lmtp 389527 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/relay 389526 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/smtp 389534 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/retry 389541 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/scache 389525 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/proxywrite 389537 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/local 389514 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/tlsmgr 389535 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/discard 389522 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/verify 389516 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/rewrite 389517 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/private/bounce 389523 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/public/flush 389511 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/public/cleanup 389532 0 srw-rw-rw- 1 postfix postfix 0 Jul 18 13:22 /var/spool/postfix/public/showq [root@MyServer ~]# find /tmp -type b -ls [root@MyServer ~]# find /tmp -type c -ls [root@MyServer ~]# find /tmp -type p -ls
组合条件:
与:-a
或:-o
非:-not, !
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)
找出/tmp目录下,属主不是root,且文件名不是fstab的文件;
find /tmp \( -not -user root -a -not -name ‘fstab‘ \) -ls
find /tmp -not \( -user root -o -name ‘fstab‘ \) -ls
查找根目录下面没有属主和没有属组的用-o来组合
[root@MyServer ~]# find / -nouser -o -nogroup find: `/proc/21078/task/21078/fd/5‘: No such file or directory find: `/proc/21078/task/21078/fd/5‘: No such file or directory find: `/proc/21078/task/21078/fdinfo/5‘: No such file or directory find: `/proc/21078/task/21078/fdinfo/5‘: No such file or directory find: `/proc/21078/fd/5‘: No such file or directory find: `/proc/21078/fd/5‘: No such file or directory find: `/proc/21078/fdinfo/5‘: No such file or directory find: `/proc/21078/fdinfo/5‘: No such file or directory /tmp/hosts /var/spool/mail/user2 /var/spool/mail/user1 /home/user1 /home/user1/a.txt /home/user1/.bashrc /home/user1/.bash_profile /home/user1/.bash_history /home/user1/.bash_logout /home/user2 /home/user2/.bashrc /home/user2/.bash_profile /home/user2/.bash_logout [root@MyServer ~]# find /home/ -nouser -o -nogroup -ls 这里有问题 [root@MyServer ~]# find /home/ -nouser -o -nogroup /home/user1 /home/user1/a.txt /home/user1/.bashrc /home/user1/.bash_profile /home/user1/.bash_history /home/user1/.bash_logout /home/user2 /home/user2/.bashrc /home/user2/.bash_profile /home/user2/.bash_logout
[root@MyServer ~]# find /home/ \( -nouser -o -nogroup \) -ls 32065 4 drwx------ 2 500 500 4096 Jul 18 20:38 /home/user1 32070 4 -rw-rw-r-- 1 500 500 5 Jul 18 20:38 /home/user1/a.txt 32066 4 -rw-r--r-- 1 500 500 124 Sep 26 2014 /home/user1/.bashrc 32067 4 -rw-r--r-- 1 500 500 176 Sep 26 2014 /home/user1/.bash_profile 32069 4 -rw------- 1 500 500 60 Jul 18 20:38 /home/user1/.bash_history 32068 4 -rw-r--r-- 1 500 500 18 Sep 26 2014 /home/user1/.bash_logout 160321 4 drwx------ 2 502 502 4096 Jul 17 22:03 /home/user2 160322 4 -rw-r--r-- 1 502 502 124 Sep 26 2014 /home/user2/.bashrc 160323 4 -rw-r--r-- 1 502 502 176 Sep 26 2014 /home/user2/.bash_profile 160324 4 -rw-r--r-- 1 502 502 18 Sep 26 2014 /home/user2/.bash_logout
根据文件大小来查找:
-size [+|-]#UNIT
常用单位:k, M, G
#UNIT: (#-1, #]
-#UNIT:[0,#-1]
+#UNIT:(#,oo)
查看/var下面文件大约为3K
[root@MyServer ~]# find /var -size 3k -ls 390030 4 -rw-r--r-- 1 root root 2468 Dec 21 2011 /var/lib/yum/history/2011-12-21/2/config-main 389582 4 -rw-r--r-- 1 root root 2614 Apr 30 2013 /var/lib/yum/history/2011-12-21/5/config-main 390117 4 -rw-r--r-- 1 root root 2630 Jul 18 20:13 /var/lib/yum/history/2011-12-21/11/config-main 390197 4 -rw-r--r-- 1 root root 2606 Jul 18 20:28 /var/lib/yum/history/2011-12-21/14/config-main 389743 4 -rw-r--r-- 1 root root 2605 Aug 26 2014 /var/lib/yum/history/2011-12-21/7/config-main 390177 4 -rw-r--r-- 1 root root 2636 Jul 18 20:15 /var/lib/yum/history/2011-12-21/13/config-main 389663 4 -rw-r--r-- 1 root root 2613 Apr 30 2013 /var/lib/yum/history/2011-12-21/6/config-main 390040 4 -rw-r--r-- 1 root root 2606 Jul 11 21:14 /var/lib/yum/history/2011-12-21/9/config-main 389856 4 -rw-r--r-- 1 root root 2602 Sep 28 2014 /var/lib/yum/history/2011-12-21/8/config-main 390157 4 -rw-r--r-- 1 root root 2458 Dec 21 2011 /var/lib/yum/history/2011-12-21/3/config-main 389895 4 -rw-r--r-- 1 root root 2484 Apr 30 2013 /var/lib/yum/history/2011-12-21/4/config-main 390044 4 -rw-r--r-- 1 root root 2131 Jul 18 20:13 /var/lib/yum/history/2011-12-21/12/saved_tx 390146 4 -rw-r--r-- 1 root root 2634 Jul 18 20:13 /var/lib/yum/history/2011-12-21/12/config-main 390095 4 -rw-r--r-- 1 root root 2607 Jul 17 21:17 /var/lib/yum/history/2011-12-21/10/config-main 389618 4 -rw-r--r-- 1 root root 2390 Dec 21 2011 /var/lib/yum/history/2011-12-21/1/config-main 389453 4 -rw------- 1 root root 2415 Sep 28 2014 /var/log/secure-20140928
[root@MyServer ~]# find /var -size 3k /var/lib/yum/history/2011-12-21/2/config-main /var/lib/yum/history/2011-12-21/5/config-main /var/lib/yum/history/2011-12-21/11/config-main /var/lib/yum/history/2011-12-21/14/config-main /var/lib/yum/history/2011-12-21/7/config-main /var/lib/yum/history/2011-12-21/13/config-main /var/lib/yum/history/2011-12-21/6/config-main /var/lib/yum/history/2011-12-21/9/config-main /var/lib/yum/history/2011-12-21/8/config-main /var/lib/yum/history/2011-12-21/3/config-main /var/lib/yum/history/2011-12-21/4/config-main /var/lib/yum/history/2011-12-21/12/saved_tx /var/lib/yum/history/2011-12-21/12/config-main /var/lib/yum/history/2011-12-21/10/config-main /var/lib/yum/history/2011-12-21/1/config-main /var/log/secure-20140928
根据时间戳:
以“天”为单位;
-atime [+|-]#,
#: [#,#+1)
+#: [#+1,oo]
-#: [0,#)
-mtime
-ctime
以“分钟”为单位:
-amin
-mmin
-cmin
amin是n分钟内被访问,cmin是n分钟内被修改
-amin n
File was last accessed n minutes ago.
-cmin n
File‘s status was last changed n minutes ago.
[root@MyServer ~]# date Mon Jul 18 21:27:13 CST 2016 [root@MyServer ~]# find /etc -mtime 4 -ls [root@MyServer ~]# find /etc -mtime 5 -ls [root@MyServer ~]# find /etc -mtime 2 -ls [root@MyServer ~]# find /etc -mtime 1 -ls 129829 4 drwxr-xr-x 2 root root 4096 Jul 17 10:08 /etc/profile.d 133857 4 -rw-r--r-- 1 root root 64 Jul 17 10:08 /etc/profile.d/welcome.sh [root@MyServer ~]# find /root -amin 1 -ls 12 4 dr-xr-x--- 3 root root 4096 Jul 18 21:30 /root
根据权限查找:
-perm [/|-]MODE
MODE: 精确权限匹配
/MODE:任何一类(u,g,o)对象的权限中只要能一位匹配即可;
-MODE:每一类对象都必须同时拥有为其指定的权限标准;
查找/root 权限为777的文件
[root@MyServer ~]# find /root -perm 777 /root/aa.txt [root@MyServer ~]# ll -a total 60 dr-xr-x---. 3 root root 4096 Jul 18 21:30 . dr-xr-xr-x. 22 root root 4096 Jul 18 13:22 .. -rw-r--r-- 1 root root 0 Jul 17 11:09 3 -rwxrwxrwx 1 root root 27 Jul 12 20:43 aa.txt -rw-r--r-- 1 root root 9 Jul 18 21:30 ab.txt -rw-r--r-- 1 root root 0 Jul 17 21:13 a_c -rw-r--r-- 1 root root 0 Jul 17 21:13 a_d -rw-------. 1 root root 8364 Jul 17 23:42 .bash_history -rw-r--r--. 1 root root 18 May 20 2009 .bash_logout -rw-r--r-- 1 root root 185 Jul 18 20:20 .bash_profile -rw-r--r-- 1 root root 206 Jul 12 21:00 .bashrc -rw-r--r-- 1 root root 0 Jul 17 21:13 b_c -rw-r--r-- 1 root root 0 Jul 17 21:13 b_d -rw-r--r--. 1 root root 100 Sep 23 2004 .cshrc -rwxr-xr-x 1 root root 63 Jul 17 22:01 nnnn.sh drwxr-xr-x 2 root root 4096 Jul 16 21:34 shell -rw-r--r--. 1 root root 129 Dec 4 2004 .tcshrc -rw-r--r-- 1 root root 0 Jul 17 21:20 tfile-2016-07-17-21-20-46 -rw------- 1 root root 3852 Jul 18 20:20 .viminfo
所属主有写权限
[root@MyServer ~]# find /root -perm +222 -ls 12177 0 -rw-r--r-- 1 root root 0 Jul 17 21:13 /root/a_c 6873 4 -rw------- 1 root root 3852 Jul 18 20:20 /root/.viminfo 12186 0 -rw-r--r-- 1 root root 0 Jul 17 21:20 /root/tfile-2016-07-17-21-20-46 12173 4 -rw-r--r-- 1 root root 206 Jul 12 21:00 /root/.bashrc 12188 4 -rwxr-xr-x 1 root root 63 Jul 17 22:01 /root/nnnn.sh 3113 4 drwxr-xr-x 2 root root 4096 Jul 16 21:34 /root/shell 12171 4 -rwxr--r-- 1 root root 28 Jul 16 20:53 /root/shell/aaaa.sh 12172 4 -rwxr-xr-x 1 root root 45 Jul 11 21:16 /root/shell/1.sh 12176 4 -rwxr-xr-x 1 root root 166 Jul 16 21:34 /root/shell/s.sh 12560 4 -rw-r--r-- 1 root root 185 Jul 18 20:20 /root/.bash_profile 3955 12 -rw------- 1 root root 8364 Jul 17 23:42 /root/.bash_history 6872 4 -rw-r--r-- 1 root root 18 May 20 2009 /root/.bash_logout 12180 0 -rw-r--r-- 1 root root 0 Jul 17 21:13 /root/b_d 12178 0 -rw-r--r-- 1 root root 0 Jul 17 21:13 /root/a_d 12179 0 -rw-r--r-- 1 root root 0 Jul 17 21:13 /root/b_c 12175 0 -rw-r--r-- 1 root root 0 Jul 17 11:09 /root/3 12174 4 -rwxrwxrwx 1 root root 27 Jul 12 20:43 /root/aa.txt 3568 4 -rw-r--r-- 1 root root 9 Jul 18 21:30 /root/ab.txt 6876 4 -rw-r--r-- 1 root root 129 Dec 4 2004 /root/.tcshrc 6875 4 -rw-r--r-- 1 root root 100 Sep 23 2004 /root/.cshrc
[root@MyServer aaa]# chmod 100 aaaa.txt [root@MyServer aaa]# chmod 002 bbbaa.txt [root@MyServer aaa]# ll total 8 ---x------ 1 root root 4 Jul 18 21:46 aaaa.txt --------w- 1 root root 4 Jul 18 21:46 bbbaa.txt [root@MyServer aaa]# find /root/aaa/ -perm +100 -ls 12653 4 drwxr-xr-x 2 root root 4096 Jul 18 21:46 /root/aaa/ 12654 4 ---x------ 1 root root 4 Jul 18 21:46 /root/aaa/aaaa.txt [root@MyServer aaa]# find /root/aaa/ -perm +002 -ls 12655 4 --------w- 1 root root 4 Jul 18 21:46 /root/aaa/bbbaa.txt
处理动作:
-print:默认的处理动作,显示至屏幕;
-ls:类似于对查找到的文件执行“ls -l”命令;
-delete:删除查找到的文件;
-fls /path/to/somefile:查找到的所有文件的长格式信息保存至指定文件中;
-ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令;
对于每个文件执行命令之前,都会交互式要求用户确认;
-exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令;
{}: 用于引用查找到的文件名称自身;
注意:find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令;
有些命令不能接受过多参数,此时命令执行可能会失败;另一种方式可规避此问题:
find | xargs COMMAND
比如/tmp下面hosts没有属于哪一个用户,只有uid及gid
[root@MyServer aaa]# ll /tmp/ total 24 -rw-r--r-- 1 500 500 158 Jul 17 13:49 hosts -rw-r--r-- 1 root root 884 Jul 17 21:06 inittab drwxr-xr-x 14 root root 4096 Jul 17 21:14 mylinux drwxr-xr-x 10 root root 4096 Jul 17 21:21 mytest1 drwxr-xr-x 21 root root 4096 Jul 17 21:21 mytest2 drwxr-xr-x 2 root root 4096 Jul 17 21:22 mytest3 -rw-------. 1 root root 0 Dec 21 2011 yum.log [root@MyServer aaa]# find /tmp -nouser -exec chown root {} \; [root@MyServer aaa]# find /tmp -nouser [root@MyServer aaa]# find /tmp -nogroup /tmp/hosts [root@MyServer aaa]# find /tmp -nogroup -ok chown :root {} \; < chown ... /tmp/hosts > ? y [root@MyServer aaa]# find /tmp -nogroup [root@MyServer aaa]#
练习:
1、查找/var目录下属主为root,且属组为mail的所有文件或目录;
# find /var -user root -group mail
2、查找/usr目录下不属于root、bin或hadoop的所有文件或目录;
# find /usr -not -user root -a -not -user bin -a -not -user hadoop
# find /usr -not \( -user root -o -user bin -o -user hadoop \)
3、查找/etc目录下最周一周内其内容修改过,同时属主不为root,也不是hadoop的文件或目录;
# find /etc -mtime -7 -a -not -user root -a -not -user hadoop
# find /etc/ -mtime -7 -a -not \( -user root -o -user hadoop \)
4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件或目录;
# find / -nouser -a -nogroup -a -atime -7
5、查找/etc目录下大于1M且类型为普通文件的所有文件;
# find /etc -size +1M -type f
6、查找/etc目录下所有用户都没有写权限的文件;
# find /etc -not -perm /222
7、查找/etc目录下至少有一类用户没有执行权限的文件;
# find /etc -not -perm -111
8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件;
# find /etc/init.d -perm -113
本文出自 “梁小明的博客” 博客,请务必保留此出处http://7038006.blog.51cto.com/7028006/1827537
原文地址:http://7038006.blog.51cto.com/7028006/1827537