Linux基础知识之find命令详解

在运维人员操作系统时,要接触大量的文件,为了避免忘记文件存放位置的尴尬,就需要我们有一种文件查找工具的帮忙,下面是两个文件查找工具的详解,locate以及find,分别分享给大家。

第一款工具: Locate

locate - find files by name

locate的工作依赖于事先构建好的索引库;查找文件时,直接搜索索引库里记载的文件的位置;

索引库的构建:

 系统自动实现(周期性任务);

 手动更新数据库(updatedb),但是索引构建的过程需要遍历整个文件系统,极其耗费系统资源;

    updatedb - update a database for mlocate;

工作特性:

  查询速度快,但不一定精确,无法匹配到数据库创建后的创建文件;

  非实时查找,不能实时反馈当前文件系统上的文件状态 ;

扫描二维码关注公众号,回复: 1981976 查看本文章

使用方法:

  locate [OPTIONS] FILE..

   选项:

   -c:统计查找结果的数量

    -b:只匹配路径中的基名

   -r:基于基本正则表达式写匹配模式

1
2
3
4
5
6
7
8
9
10
11
12
[root@linuxidc ~] # locate -c  "passwd"
165
[root@linuxidc ~] # locate -c  "inittab"
4
  
[root@linuxidc ~] # locate -b  "inittab"
/etc/inittab
/usr/local/share/man/zh_CN/man5/inittab .5
/usr/share/augeas/lenses/dist/inittab .aug
/usr/share/vim/vim74/syntax/inittab .vim
  
[root@linuxidc ~] # locate -r  "/passwd

第二款工具:Find

find: find - search for files in a directory hierarchy

工作方式:通过遍历指定起始路径下文件系统层级结构完成文件查找;

工作特性:

  查找速度慢;

  精确查找;

  实时查找;

使用方法:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

fing [OPTIONS] [查找起始路径] [查找条件] [处理动作]

查找起始路径:指定具体搜索目标起始路径;默认当前目录;

查找条件:指定的查找标准,可以根据文件名,大小,类型,从属关系,权限等标准,默认为指定目录下的所有条件

处理动作:对符合条件的文件作出的操作,例如删除等操作,默认为输出至标准输出

查找条件说明: 

以表达式的形式,包含选项和测试条件

  测试:结果通常为布尔型数据("true""fales")

   (1)根据文件名查找 注意:支持glob风格的通配符

    -name "pattern":区分大小写

    -iname "pattern":不区分名字的大小写,

    -regex "patten":基于正则表达式模式查找文件,匹配是整个路径 ,而非其名;

        wKioL1eEFy2hLWJ4AABrgXSwqs8040.png   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        [root@linuxidc ~] # find /etc -iname "passwd"
        /etc/passwd
        /etc/pam .d /passwd
        [root@linuxidc ~] # find /etc -iname "*passwd"
        /etc/passwd
        /etc/pam .d /passwd
        /etc/security/opasswd
        [root@linuxidc ~] # find /etc -iname "passwd*"
        /etc/passwd
        /etc/passwd-
        /etc/pam .d /passwd
        [root@linuxidc ~] # find /etc -iname "passwd?"
        /etc/passwd-
        [root@linuxidc ~] # find /etc -iname "passwd[[:place:]]"
        [root@linuxidc ~] #

   (2)根据文件从属关系查找

      -user username:查找属主指定用户的所有文件;

      -group groupname: 查找属组指定用户的所有文件;

     -uid UID:查找属主指定UID的所有文件;

     -gid GID:查找属组指定GID的所有文件;        #find /etc  -gid 5000

     -nouser:查找没有属主的文件                     #find /etc  -nouser

     -ngroup:查找没有属组的文件                    #find /etc  -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
        [root@linuxidc ~] # find /var/tmp/ -user           #属主查找
        /var/tmp/test/a .centos
        /var/tmp/test/b .centos
        
        [root@linuxidc ~] # find /var/tmp/ -group mygrp -ls      #属组查找
        drwxrwsr-t   2 root     mygrp          66 Jul 31 04:43  /var/tmp/test
        -rw-rw-r--   1 centos   mygrp          10 Jul 31 04:43  /var/tmp/test/b .centos
        -rw-rw-r--   1    mygrp           0 Jul 31 04:43  /var/tmp/test/b .fedora
         
        [root@linuxidc ~] # id  centos                            #UID查找
        uid=1001(centos) gid=1001(centos)  groups =1001(centos),1003(mygrp)
        [root@linuxidc ~] # find /var/tmp/ -uid 1001
        /var/tmp/test/a .centos
        /var/tmp/test/b .centos
          
        [root@linuxidc ~] # tail -n2 /etc/group                  #GID查找
        mygrp:x:1003:centos,fedora
        hodoop:x:1004:
        [root@linuxidc ~] # find /var/tmp/ -gid 1003 
        /var/tmp/test
        /var/tmp/test/b .centos
        /var/tmp/test/b .fedora
          
        [root@linuxidc ~] # find /etc  -nouser                #没有属主
        [root@linuxidc ~] # find /etc  -nogroup                #没有属组

   (3)根据文件类型查找

     -trpe TYPE: 组合动作-ls使用,查看文件的详细信息

      f:普通文件

      d:目录   

      l:链接文件

      b:块设备文件

      c:字符设备文件

      s:套接字文件

      p:管道文件

      组合测试:

      与: -a 默认组合操作逻辑:  二者同时成立

      或: -o  符合其中一项即可

      非: -not 或"!"  取反      

       !A -a !B=!(A -o B)

       !A -o !B=!(A -a B)

   练习: 找出/etc/下 属主非root的文件,且文件名中不包含fstab    

1
2
3
4
[root@linuxidc ~] # find /etc/   !  -user root -ls -a -not -name "fatab"
1233465    0 drwx------   2 polkitd  root           63 7月 19 19:07  /etc/polkit-1/rules .d
34785013    8 -rw-------   1 tss      tss          7046 11月 21  2015  /etc/tcsd .conf
35135850    0 drwx--x--x   2 sssd     sssd            6 11月 20  2015  /etc/sssd

   (4)根据文件大小查找 

     -siza[+|-]#UNIT: 常用单位:k,m,g

      #UNIT: (#-1,#]     等于数字

      -#UNIT:[0,#-1)   小于数字

      +#UNIT:(#,00)    大于数字

      -empty: 查找空文件。

1
2
3
4
5
6
7
8
9
[root@linuxidc var] # find /etc/ -size -4k -a -exec ls -lh {} \;
-rw-r--r--. 1 root root 465 7月  19 18:59  /etc/fstab
-rw-------. 1 root root 0 7月  19 18:59  /etc/crypttab
lrwxrwxrwx. 1 root root 17 7月  19 18:59  /etc/mtab  ->  /proc/self/mounts
-rw-r--r-- 1 root root 228 7月  30 18:31  /etc/resolv .conf
  
[root@linuxidc usr] # find /etc/ -size 4k -a -exec ls -lh {} \;
  
[root@linuxidc usr] # find /etc/ -size 4k -a -exec ls -lh {} \;

   (5)根据时间戳查找

     以天为单位

       -atime   [+|-]# : [#,#-1] 

       -mtime                           

       -ctime

    以分钟为单位

       -amin

       -mmin

       -cmin

   (6)根据权限查找

     -perm [/|-]mode:

        mode:精确查找 -perm -664 文件权限正好符合mode(mode为文件权限的八进制表示)。

       /mode:任何一类用户(u,g,o)权限中的任何一位(rwx)符合条件即满足  文件权限部分符合mode

         9位权限之间存在"或"关系

       -mode:每一类用户(u,g,o)的权限中的每一位(e,w,x)同时符合条件即满足;

         9位权限之间存在"与"关系文件权限完全符合mode。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@linuxidc ~] # find . -perm 644
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
  
[root@linuxidc ~] # find . -perm -644
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./.cache /abrt
./.cache /abrt/applet_dirlist
  
[root@linuxidc ~] # find /tmp/ -perm /222 
/tmp/
/tmp/log
/tmp/log/tallylog
/tmp/log/lastlog

 

处理动作

 -print:输出至标准输出,默认的动作

 -ls:类似于对查找到的文件执行"ls -l"命令,输出文件的详细信息.

 -delete:删除查找到的文件;

 -fls /path/to/SomeFIle:把查找到的所有文件的长格式信息保存至指定文件中

 -ok COMMAND {} \;  :对查找的每个文件执行有COMMAND表示的命令;每次操作都有用户进行确认;

 -exec COMMAND {} \; :对查找的每个文件执行有COMMAND表示的命令;

   注意: find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面的命令;

   但是有些命令不能接受过长的参数,此时命令执行会失败;另一种方式可规避此问题.

      find | xargs COMMAND       学习xargs命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@linuxidc ~] # find /tmp -name "qwe.sh"  -type f -delete
[root@linuxidc ~] # ls /tmp/
hogsuspend  log
   
[root@linuxidc ~] # find /etc/ ! -perm /222  -type f -fls /tmp/qwe.sh
[root@linuxidc ~] # cat /tmp/qwe.sh
33885564  196 -r--r--r--   1 root     root       198453 7月 19 19:02  /etc/pki/ca-trust/extracted/java/cacerts
67923558  352 -r--r--r--   1 root     root       359773 7月 19 19:02  /etc/pki/ca-trust/extracted/openssl/ca-bundle .trust.crt
  
[root@linuxidc ~] # find /etc/ ! -perm /222  -type f -ok ls -lh {} \;
ls  ...  /etc/pki/ca-trust/extracted/java/cacerts  > ? y
-r--r--r--. 1 root root 194K 7月  19 19:02  /etc/pki/ca-trust/extracted/java/cacerts
ls  ...  /etc/pki/ca-trust/extracted/openssl/ca-bundle .trust.crt > ? y
-r--r--r--. 1 root root 352K 7月  19 19:02  /etc/pki/ca-trust/extracted/openssl/ca-bundle .trust.crt
ls  ...  /etc/pki/ca-trust/extracted/pem/tls-ca-bundle .pem > ? y
  
[root@linuxidc ~] # find /etc/ ! -perm /222  -type f -exec ls -lh {} \;
-r--r--r--. 1 root root 194K 7月  19 19:02  /etc/pki/ca-trust/extracted/java/cacerts
-r--r--r--. 1 root root 352K 7月  19 19:02  /etc/pki/ca-trust/extracted/openssl/ca-bundle .trust.crt
-r--r--r--. 1 root root 261K 7月  19 19:02  /etc/pki/ca-trust/extracted/pem/tls-

xargs命令:

    该命令的主要功能是从输入中构建和执行shell命令。       
     在使用find命令的-exec选项处理匹配到的文件时,   find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现 溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。  
    find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。  
    在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;  
    而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。
    #查找当前目录下的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件。 

1
2
3
4
5
root@linuxidc ~] # find . -type f -print | xargs file
    . /users2 :        ASCII text
    . /datafile3 :      empty
    . /users :          ASCII text
    . /test . tar .bz2:  bzip2  compressed data, block size = 900k

   #回收当前目录下所有普通文件的执行权限。

1
2
3
4
5
6
7
8
[root@linuxidc ~] # find . -type f -print|xargs chmod a-x
[root@linuxidc ~] # ll 
总用量 8
-rw-r--r-- 1 root root    0 7月  30 11:38 ad
-rwSr--r-- 1 root root 2620 7月  24 10:10  passwd .bak
drwxr-xr-x 2 root root    6 7月  30 12:31 qwe
drwxr-xr-x 2 root root 4096 7月  29 20:10 shell
drwxr-xr-x 2 root root  125 7月  30 16:02 shlianxi

    #在当面目录下查找所有普通文件,并用grep命令在搜索到的文件中查找hostname这个词 

1
[root@linuxidc ~] # find /etc -type f -print | xargs grep "hostname"

练习:

1.查找/var/目录下属主为root,且数组为mall的所有文件或目录;   

1
2
3
[root@linuxidc ~] # find /var/ -user root -a -group mail
/var/spool/mail
/var/spool/mail/root

3.查找/etc/目录下最近一周内其文件修改过,且属主不是root用户也不是hadoop用户的文件或目录 

1
[root@linuxidc ~] # find / -mtime -7 -a ! -user root -a ! -user hadoop

4.查找当前系统上没有属主或属组,且最近一周内曾被访问的文件或目录

1
2
[root@linuxidc ~] # find / -nouser -o -nogroup -a -atime -7
[root@linuxidc ~] #find / \(-nouser -o -nogroup\) -atime -7 -ls

5,查找/etc/目录下大于1M且类型为普通文件的所有文件  

1
2
[root@linuxidc ~] # find  /etc/ -size +1M -a -type f -ls
[root@linuxidc ~] # find /etc/ -size +1M  -type f -exec ls -lh {} \;

6,查找/etc/目录下所有用户都没有写权限的文件

1
[root@linuxidc ~] # find  /etc/ -size +1M -a -type f -ls

7.查找/etc/目录下至少有一类用户没有执行权限的文件      

1
[root@linuxidc ~] # find /etc/ !  -perm -111 -type -f  -ls

8.查找/etc/init.d目录下,所有用户都有执行权限,且其他用户有写权限的所有文件. 

1
2
  [root@linuxidc ~] #find /etc/ -perm -111 -a -perm  -002  -type f -ls
  [root@linuxidc ~] #find /etc/ -perm -113 -type f -ls

猜你喜欢

转载自www.linuxidc.com/Linux/2016-08/133815.htm