目录

locate

find

-name,-iname

--regex

-user,-group,-uid,gid,-nouser,-nogroup

-type  

-size  -+      #-1

-perm  -+

-atime,-mtime,-ctime,-amin,-mmin,-cmin    -+ #+1

-print,-ls,-exec,-ok     


     根据文件的各种属性去找到相应的文件

、locate

      根据此前updatedb命令生成的数据库来完成文件查找

      查找速度快,非实时查找,结果不精确

      模糊查找:在当前系统上匹配文件路径查找

locate [OPTION]... [PATTERN]...

     -b     查找文件名中包含目标字符的文件,而不是路径中包含的

[root@Node3 ~]# locate test|head
/data/test
/etc/yum/pluginconf.d/fastestmirror.conf
/home/testbash
/home/testbash/.bash_logout
/home/testbash/.bash_profile
/home/testbash/.bashrc
/home/testbash/.gnome2
/home/testbash/.mozilla
/home/testbash/.mozilla/extensions
/home/testbash/.mozilla/plugins
[root@Node3 ~]# locate -b test|head
/data/test
/etc/yum/pluginconf.d/fastestmirror.conf
/home/testbash
/lib/alsa/init/test
/lib/modules/2.6.32-431.el6.x86_64/kernel/arch/x86/kernel/test_nx.ko
/lib/modules/2.6.32-431.el6.x86_64/kernel/crypto/async_tx/raid6test.ko
/lib64/rsyslog/omtesting.so
/root/test
/root/test.lg
/root/test.sh

二、find

          通过遍历指定的目标目录,实时查找符合指定属性的文件

          精确匹配,速度略慢

find [OPTINS] [查找路径] [查找条件] [处理动作]

查找路径:默认为当前路径

查找条件:默认为指定路径下的所有文件

处理动作:默认为打印至屏幕

[root@Node3 test]# find
.
./b
./c
./a
[root@Node3 test]# find .
.
./b
./c
./a
[root@Node3 test]# find /root/test
/root/test
/root/test/b
/root/test/c
/root/test/a

查找条件:

1、根据文件名

      -name  "文件名"     支持文件名通配*,?,[],[^]

      -iname  "文件名"     文件名不区分大小写

[root@Node3 ~]# find /etc -name passwd
/etc/pam.d/passwd
/etc/passwd
[root@Node3 ~]# find /etc -name p*d
/etc/pam.d
/etc/pam.d/passwd
/etc/xdg/menus/preferences-merged
/etc/xdg/menus/preferences-post-merged
/etc/popt.d
/etc/cups/ppd
/etc/passwd
/etc/profile.d
/etc/audisp/plugins.d
/etc/yum/pluginconf.d
/etc/yum/protected.d
/etc/pm/power.d

2、根据文件的属主,属组查找

      -user  USERNAME         根据文件属主查找

      -group  USERNAME      根据文件属组查找

      -uid  UID     根据指定UID查找         #根据uid,gid查找并不是说此文件没属主

      -gid  GID     根据指定GID查找

      -nouser       查找没有属主的文件     #这个才是没属主

      -nogroup    查找没有属组的文件

[root@Node3 ~]# ll /home/xj
总用量 0
[root@Node3 ~]# find /home -user xj    #会显示隐藏的文件
/home/xj
/home/xj/.bash_profile
/home/xj/.mozilla
/home/xj/.mozilla/plugins
/home/xj/.mozilla/extensions
/home/xj/.gnome2
/home/xj/.bash_logout
/home/xj/.Xauthority
/home/xj/.bash_history
/home/xj/.viminfo
/home/xj/.bashrc

#-gid,-uid
[root@Node3 ~]# find /home -gid 2021
/home/bash
/home/bash/.bash_profile
/home/bash/.mozilla
/home/bash/.mozilla/plugins
/home/bash/.mozilla/extensions
/home/bash/.gnome2
/home/bash/.bash_logout
/home/bash/.bashrc
[root@Node3 ~]# ls -al /home/bash
总用量 28
drwx------   4 bash bash 4096 12月  1 16:36 .
drwxr-xr-x. 15 root root 4096 12月  1 16:56 ..
-rw-r--r--   1 bash bash   18 7月  18 2013 .bash_logout
-rw-r--r--   1 bash bash  176 7月  18 2013 .bash_profile
-rw-r--r--   1 bash bash  124 7月  18 2013 .bashrc
drwxr-xr-x   2 bash bash 4096 11月 12 2010 .gnome2
drwxr-xr-x   4 bash bash 4096 11月 27 15:14 .mozilla

#-nouser,-nogroup
[root@Node3 home]# find /home -nogroup
/home/mandriva
/home/mandriva/.bash_profile
/home/mandriva/.mozilla
/home/mandriva/.mozilla/plugins
/home/mandriva/.mozilla/extensions
/home/mandriva/.gnome2
/home/mandriva/.bash_logout
/home/mandriva/.bashrc
[root@Node3 home]# find /home -nouser
/home/mandriva
/home/mandriva/.bash_profile
/home/mandriva/.mozilla
/home/mandriva/.mozilla/plugins
/home/mandriva/.mozilla/extensions
/home/mandriva/.gnome2
/home/mandriva/.bash_logout
/home/mandriva/.bashrc
/home/xiena
/home/xiena/.bash_profile
/home/xiena/.mozilla
/home/xiena/.mozilla/plugins
/home/xiena/.mozilla/extensions
/home/xiena/.gnome2
/home/xiena/.bash_logout
/home/xiena/.bash_history
/home/xiena/.bashrc
[root@Node3 home]# ll -la /home/{xiena,mandriva}
/home/mandriva:
总用量 28
drwx------   4 1005 2015 4096 11月 30 06:56 .
drwxr-xr-x. 14 root root 4096 12月  1 21:26 ..
-rw-r--r--   1 1005 2015   18 7月  18 2013 .bash_logout
-rw-r--r--   1 1005 2015  176 7月  18 2013 .bash_profile
-rw-r--r--   1 1005 2015  124 7月  18 2013 .bashrc
drwxr-xr-x   2 1005 2015 4096 11月 12 2010 .gnome2
drwxr-xr-x   4 1005 2015 4096 11月 27 15:14 .mozilla

/home/xiena:
总用量 32
drwx------   4  502 xiena 4096 11月 30 07:20 .
drwxr-xr-x. 14 root root  4096 12月  1 21:26 ..
-rw-------   1  502 xiena   28 11月 30 07:20 .bash_history
-rw-r--r--   1  502 xiena   18 7月  18 2013 .bash_logout
-rw-r--r--   1  502 xiena  176 7月  18 2013 .bash_profile
-rw-r--r--   1  502 xiena  124 7月  18 2013 .bashrc
drwxr-xr-x   2  502 xiena 4096 11月 12 2010 .gnome2
drwxr-xr-x   4  502 xiena 4096 11月 27 15:14 .mozilla

3、匹配路径的BRE

      -regex  "PATTERN"       以PATTERN匹配整个文件路径字符串,而不仅仅是文件名

[root@Node3 ~]# find /etc -regex 'p.+d.+'
[root@Node3 ~]# 
[root@Node3 ~]# find /etc -regex '.*p.*d'|head
/etc/dhcp/dhclient.d
/etc/bash_completion.d
/etc/NetworkManager/dispatcher.d
/etc/pam.d
/etc/pam.d/gdm-password
/etc/pam.d/vmtoolsd
/etc/pam.d/sshd
/etc/pam.d/crond
/etc/pam.d/passwd
/etc/pam.d/atd
[root@Node3 ~]# find /etc -regex '.+p.+d.+'|head  
/etc/vmware-caf/pme/install/upgrade.sh
/etc/vmware-caf/pme/install/preupgrade.sh
/etc/vmware-caf/pme/install/caf-dbg.sh
/etc/vmware-caf/pme/install/stopAndRemoveServices.sh
/etc/dhcp/dhclient.d
/etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf
/etc/fonts/conf.d/25-ttf-arphic-uming-render.conf
/etc/fonts/conf.d/25-no-bitmap-dist.conf
/etc/fonts/conf.d/25-ttf-arphic-ukai-render.conf
/etc/fonts/conf.d/90-ttf-arphic-ukai-embolden.conf

4、根据文件类型 

-type TYPE    根据文件类型查找

        f   普通文件 

       d   目录文件

       l   符号链接文件

       b 块设备文件,c 字符设备文件  p 命令名管道文件  s  套接字文件

              

5、根据文件大小

  -size   [+|-]#UNIT            常用单位K,M,G默认为B,#为数字

这里的大小有点诡异:

             #UNIT    匹配    #-1 < x <= #     例:#UNIT=2k表示:1.01k到2k        就是会约等

           -#UNIT   匹配      x <= #-1         例:-2k表示:小于等于1k

           +#UNIT  匹配       x > #              例:+2k表示:大于2K不包括2K

root@Node3 ~]# find /tmp -size 2k 
/tmp/vmware-root/vmware-apploader-42709.log
[root@Node3 ~]# ls /tmp/vmware-root/vmware-apploader-42709.log -l  #不到2k
-rw-r----- 1 root root 1773 11月 28 11:03 /tmp/vmware-root/vmware-apploader-42709.log

6、根据时间戳来查找      

以天为单位:

      -atime [+|-]#      

             #:    #=< x < #+1          例如:2表示:大于3天但不到4天

           -#:   x < #                                          小于3天

          +#:  x >= #+1                                    大于等于4天


       -mtime,-ctime

以分钟为单位:                  #规则和上面的一样

       -amin [+|-]#

       -mmin,-cmin


7、根据权限查找

 -perm [+|-] MODE

      MODE:与MODE精确匹配

    +MODE任何一类用户只要能包含对其相应用户指定的任何一位权限即可(用户和权限要对应匹配)   

                          例:+555  表示只要目录文件只要属主或数组或任何人中只要有W或X就匹配

     -MODE:每一类用户的权限都包含对其指定的所以权限

                               -222: 666权限的文件符合   664不符合

注意:这里的权限只能用数字表示,不能使用rwx

[root@Node3 src]# ll
总用量 12
-rw-r--r-- 1 root root  0 12月  1 22:02 2.out
-rw-r--r-- 1 root root  0 12月  1 22:07 aa.txt
-rwxrwxrwx 1 root xj    2 11月 29 21:06 EOF
lrwxrwxrwx 1 root xj   22 11月 29 16:37 grub.conf -> ../boot/grub/grub.conf
-rwxrwxrwx 1 root xj    0 11月 29 21:15 help
-rwxrwxrwx 1 root xj   12 11月 29 21:03 sb
--w------- 1 root xj    4 11月 29 20:43 test
--------w- 1 root root  0 12月  2 09:29 test1
-----w---- 1 root xj    0 11月 29 11:16 test.sh
[root@Node3 src]# !find
find /tmp/src -perm +2 -ls       #注意这里这1个2是表示o位上的权限,只有o位上至少有w的权限才被匹配到,其它位上有w的权限不行。
522269    4 drwxrwxrwx   2 root     xj           4096 12月  2 09:29 /tmp/src
522320    0 --------w-   1 root     root            0 12月  2 09:29 /tmp/src/test1
522319    4 -rwxrwxrwx   1 root     xj             12 11月 29 21:03 /tmp/src/sb
522322    0 -rwxrwxrwx   1 root     xj              0 11月 29 21:15 /tmp/src/help
522314    0 lrwxrwxrwx   1 root     xj             22 11月 29 16:37 /tmp/src/grub.conf -> ../boot/grub/grub.conf
522321    4 -rwxrwxrwx   1 root     xj              2 11月 29 21:06 /tmp/src/EOF
[root@Node3 src]# find /tmp/src -perm +222 -ls   
                  #这里就表示任何一类用户位上有w即可
522269    4 drwxrwxrwx   2 root     xj           4096 12月  2 09:29 /tmp/src
522318    0 -rw-r--r--   1 root     root            0 12月  1 22:02 /tmp/src/2.out
522265    0 -----w----   1 root     xj              0 11月 29 11:16 /tmp/src/test.sh
522320    0 --------w-   1 root     root            0 12月  2 09:29 /tmp/src/test1
522319    4 -rwxrwxrwx   1 root     xj             12 11月 29 21:03 /tmp/src/sb
522322    0 -rwxrwxrwx   1 root     xj              0 11月 29 21:15 /tmp/src/help
522317    0 -rw-r--r--   1 root     root            0 12月  1 22:07 /tmp/src/aa.txt
522314    0 lrwxrwxrwx   1 root     xj             22 11月 29 16:37 /tmp/src/grub.conf -> ../boot/grub/grub.conf
522321    4 -rwxrwxrwx   1 root     xj              2 11月 29 21:06 /tmp/src/EOF
522316    4 --w-------   1 root     xj              4 11月 29 20:43 /tmp/src/test


组合查找条件

与:-a                  查找条件1  [-a]  查找条件2  [-a]  ....         #-a可以省略

或: -o                 查找条件1  -o 查找条件2  -o  ....

非:-not或-!       查找条件1  -not  查找条件2  -not  ....

[root@Node3 ~]# find /home -nouser -a -nogroup 
/home/mandriva
/home/mandriva/.bash_profile
/home/mandriva/.mozilla
/home/mandriva/.mozilla/plugins
/home/mandriva/.mozilla/extensions
/home/mandriva/.gnome2
/home/mandriva/.bash_logout
/home/mandriva/.bashrc

[root@Node3 ~]# find /tmp -not -user root 
/tmp/pulse-1PEBC08s1QXD
[root@Node3 ~]# find /tmp -not -user 0
/tmp/pulse-1PEBC08s1QXD

[root@Node3 ~]# find /tmp  -name passwd -o -name test
/tmp/src/test
/tmp/passwd

注意:

       组合条件如果后面接执行动作要带上括号(括号需要转义),括号两边要有空格,否则执行条件时,可能只有后一个条件查找到的文件会被操作


处理动作

-print               默认动作,打印至屏幕

-ls                    显示找到的文件的详细属性

-delete             删除查找到的文件

-fls /FILENAME                     查找到的文件的详细路径信息保存至指定文件中

-exec COMMAND {} \;           不会提示确定,直接操作

-ok  COMMAND {} \;             会提示确定,再操作

注意:

       find命令会一次性找到所有符合条件的文件(而不是找到一条传递一条),并一同传递给后面的命令,但有些命令不能接受过长的参数,解决办法:find |xargs COMMAND

  找到文件后把文件名改成之前文件后加上.notroot?

[root@Node3 src]# find /home -nouser
/home/mandriva
/home/mandriva/.bash_profile
/home/mandriva/.mozilla
/home/mandriva/.mozilla/plugins
/home/mandriva/.mozilla/extensions
/home/mandriva/.gnome2
/home/mandriva/.bash_logout
/home/mandriva/.bashrc
/home/xiena
/home/xiena/.bash_profile
/home/xiena/.mozilla
/home/xiena/.mozilla/plugins
/home/xiena/.mozilla/extensions
/home/xiena/.gnome2
/home/xiena/.bash_logout
/home/xiena/.bash_history
/home/xiena/.bashrc
[root@Node3 src]# find /home -nouser -exec rm {} \;
rm: 无法删除"/home/mandriva": 是一个目录
rm: 无法删除"/home/mandriva/.mozilla": 是一个目录
rm: 无法删除"/home/mandriva/.mozilla/plugins": 是一个目录
rm: 无法删除"/home/mandriva/.mozilla/extensions": 是一个目录
rm: 无法删除"/home/mandriva/.gnome2": 是一个目录
rm: 无法删除"/home/xiena": 是一个目录
rm: 无法删除"/home/xiena/.mozilla": 是一个目录
rm: 无法删除"/home/xiena/.mozilla/plugins": 是一个目录
rm: 无法删除"/home/xiena/.mozilla/extensions": 是一个目录
rm: 无法删除"/home/xiena/.gnome2": 是一个目录
[root@Node3 src]# find /home -nouser -exec rm -R {} \;


[root@Node3 src]# find /home -user bash -exec mv {} {}.bak \; #改名的先移动了目录,后面的就找不到了,所以要先移动下面的文件就好了。
find: “/home/bash”: 没有那个文件或目录
[root@Node3 src]# find /home -user bash 
/home/bash.bak
/home/bash.bak/.bash_profile
/home/bash.bak/.mozilla
/home/bash.bak/.mozilla/plugins
/home/bash.bak/.mozilla/extensions
/home/bash.bak/.gnome2
/home/bash.bak/.bash_logout
/home/bash.bak/.bashrc

后面执行的命令也可以用xargs替代:

[root@Node3 src]# ls -i
522317 111  522319 222  522318 2.out  522333 ccc  522316 test  522320 test1  522265 test.sh
[root@Node3 src]# cat 222
nihao
[root@Node3 src]# find . -inum 522319
./222
[root@Node3 src]# find . -inum 522319 -exec cat {} \;
nihao
[root@Node3 src]# find . -inum 522319|exec cat
./222
[root@Node3 src]# find . -inum 522319|-exec cat
-bash: -exec: command not found
[root@Node3 src]# find . -inum 522319|xrags cat 
-bash: xrags: command not found
[root@Node3 src]# find . -inum 522319|xargs cat 
nihao

利用i节点删除乱码文件

    find ./ -inum 12345 -exec rm {} -rf \;

[xiejun@localhost ~]$ find /tmp ! -user root -ls
1308240    0 -rw-rw-r--   1 xiejun   xiejun          0 Aug 24 14:21 /tmp/1test
[xiejun@localhost ~]$ find /tmp ! -user root -exec mv {} {}.notroot \;
[xiejun@localhost ~]$ find /tmp ! -user root -ls
1308240    0 -rw-rw-r--   1 xiejun   xiejun          0 Aug 24 14:21 /tmp/1test.notroot
[xiejun@localhost ~]$

wKioL1hAt0GTsGeaAAJ6cHn_AeI300.png

1、find /var  -user root -a -group mail 

2、find /  -nouser -o -nogroup 

  find / \( -nouser -o -nogroup \) -a -atime -3 

3、find /etc -perm -222

  find /etc -not -perm +222      #至少有一位用户有w权限的非

4、find /etc -size +1M -a -type f 

5、find /etc/init.d/ -perm -111 -a -perm +2     # -pem -113

6、find /usr -not \( -user root -o -user bin -o -user hadoop \) 

7、find /etc -not -perm -222

8、find /etc -mtime -7 -a -not \( -user root -o -user hadoop \)