系统基础之文件查找工具find

文件查找:       

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


第一款工具: Locate

locate – find files by name

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

索引库位置:/var/lib/mlocate/mlocate.db

索引库的构建:

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

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

    updatedb       – update a database for mlocate;

工作特性:

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

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

使用方法:

  locate [OPTIONS] FILE..

   选项:

    默认命令: 搜索带关键字的文件

    -i:忽略大小写

    -n #:只列举搜索结果的前#个

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
[root@wen-7 ~]# locate -c  "passwd"
165
[root@wen-7 ~]# locate -c  "inittab"
4
 
[root@wen-7 ~]# 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@wen-7 ~]# 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")

  默认多条件并行执行,相当 -a 与的关系

   

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

     -name "pattern":区分大小写 

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

        -inum  n:按inode号查找             find   -inum 21321321

        -samefile name : 相同inode号的文件      find -somefile aaa

        -links  n:链接数为n的文件    find -link   3

     -regex "patten":基于正则表达式模式查找文件,匹配是整个路径 ,而不仅仅是其名成;  find -regex ".*\.sh$"

        wKioL1eEFy2hLWJ4AABrgXSwqs8040.png   

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



   (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@w7 ~]# find /var/tmp/ -user centos          #属主查找
        /var/tmp/test/a.centos
        /var/tmp/test/b.centos
       
       [root@w7 ~]# 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 fedora   mygrp           0 Jul 31 04:43 /var/tmp/test/b.fedora
        
       [root@w7 ~]# id  centos                            #UID查找
        uid=1001(centos) gid=1001(centos) groups=1001(centos),1003(mygrp)
        [root@w7 ~]# find /var/tmp/ -uid 1001
        /var/tmp/test/a.centos
        /var/tmp/test/b.centos
         
        [root@w7 ~]# tail -n2 /etc/group                  #GID查找
        mygrp:x:1003:centos,fedora
        hodoop:x:1004:
        [root@w7 ~]# find /var/tmp/ -gid 1003 
        /var/tmp/test
        /var/tmp/test/b.centos
        /var/tmp/test/b.fedora
         
        [root@wen-7 ~]# find /etc  -nouser                #没有属主
        [root@wen-7 ~]# find /etc  -nogroup                #没有属组


   (3)根据文件类型查找

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

      f:普通文件

       d:目录    

      l:链接文件

      b:块设备文件

      c:字符设备文件

      s:套接字文件

      p:管道文件

    例: find / -name "*int*" -type d -ls

         find /  -type b

   组合测试:

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

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

         非: -not 或"!"  取反  

                        德*摩根定律:

                                非A 或  非B = 非(A 且 B)

                                非A 且  非 B= 非(A 或 B)   

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

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


    排除某目录:

        find / etc/ -path /etc/bin -a -prune  -o name "*.sh"  -print

     把/etc/bin裁掉不搜索,在剩下的目录里找名字为.sh文件

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

1
2
3
4
[root@wen-7 ~]# 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@wen-7 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@wen-7 usr]# find /etc/ -size 4k -a -exec ls -lh {} \;
 
[root@wen-7 usr]# find /etc/ -size 4k -a -exec ls -lh {} \;


      

   (5)根据时间戳查找

     以天为单位

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

                    7 [ 7,8]        [#,#+1]

                    -7 [ 0.7]      [0,#]

                            +7 [ 8,+∞]  [#+1,∞]

       -mtime                            

       -ctime

    以分钟为单位

       -amin

       -mmin

       -cmin

   (6)根据权限查找

     -perm [/|-]mode:

        mode:精确查找 -perm -664 文件权限正好符合mode(mode为文件权限的八进制表示)。0表示不关系其所在位的权限

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

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

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

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

只要当任意人有写权限时, find -perm +222就会匹配

• 只有当每个人都有写权限时, find -perm -222才会匹配

• 只有当其它人( other)有写权限时, find -perm -002才会匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@wen-7 ~]# find . -perm 644
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
 
[root@wen-7 ~]# find . -perm -644
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./.cache/abrt
./.cache/abrt/applet_dirlist
 
[root@wen-7 ~]# 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 -perm -222 -exec cp {} {}.bak \;

   注意: 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@wen-7 ~]# find /tmp -name "qwe.sh"  -type f -delete
[root@wen-7 ~]# ls /tmp/
hogsuspend  log
  
[root@wen-7 ~]# find /etc/ ! -perm /222  -type f -fls /tmp/qwe.sh
[root@wen-7 ~]# 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@wen-7 ~]# 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@wen-7 ~]# 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@wen-7 ~]# 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@wen-7 ~]# find . -type f -print|xargs chmod a-x
[root@wen-7 ~]# 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@wen-7 ~]# find /etc -type f -print | xargs grep "hostname"

 




实战演练:

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

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

  2.查找/usr目录下不属于root,bin,或hadoop的所有文件或目录,用两种方法

[root@wen-7 ~]# find /usr/ -not \( -user root -o -user bin -o -user hadoop\)[root@wen-7 ~]# find /usr/ -not -user root -a -not -user bin -a not -usr hadoop

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

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

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

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

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

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

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

1
[root@wen-7 ~]# find  /etc/ ! -perm /222

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

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

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

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

                                                  

                                                    


原创文章,作者:wencx,如若转载,请注明出处:http://www.178linux.com/36061

(0)
上一篇 2016-08-16 13:59
下一篇 2016-08-16 13:59

相关推荐

  • Find命令的使用

    Find命令的基本用法 在Linux系统中find命令是一个强大文件查找类工具,它支持根据文件名,权限,文件大小,访问以及修改时间查找,使用方法如下: find [option] [查找起始路径] [查找条件] [处理动作] 起始路径:指定查找文件或目录的路径,默认为当前目录 查找条件:可根据文件名,权限,文件大小以及从属关系查找文件 处理动作:动查找到的文…

    Linux干货 2017-08-28
  • Heartbeat-Gui V2 实现HA LAMP 搭建wordpress博客

    环境:           N F S: 172.16.0.3 (OS CentOS 7)           node1:172.16.0.5 (OS CentOS 6.7)    &nb…

    Linux干货 2016-10-27
  • 初识SElinux

    一、SElinux(Secure Enhanced Linux):安全增强的Linux        SElinux是一个在内核中实践的强制访问控制(MAC)安全性机制,由美国国家安全局NSA(The National Security Agency)和SCC(Secure Computing Co…

    Linux干货 2016-09-15
  • bash脚本进阶

     shell脚本流程控制     1.if语句 单分支: if 判断条件;  then   双分支: if 判断条件; then     条件为真的分支代码 else     条…

    Linux干货 2016-08-21
  • 基于ansible role实现LAMP平台批量部署

    前言 作为运维人员,当面对几十台或上百台服务器需要修改某个参数或部署某个平台,你将从何入手呢?ansible的出现很好的解决了这一困扰,ansible基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。本文带来的是基于Ansible Role…

    Linux干货 2015-06-11
  • Ansible的基础知识

    为什么要学习ansible:     ansible是自动化运维的一种工具,使用ansible可以一次性管理多台主机,为多台主机安装或这执行相同或者不同的操作,省去了一台台主机去重复执行相同的任务,可以使用corn选项结合本机的crontab设置本主机的任务性计划,比如,每天导出nginx的access.log和err…

    Linux干货 2016-11-03

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-19 11:57

    文章对文件查找工具常见用法有了一个完整的介绍,文件查找工具中,find命令是最重要的,几乎在所有的笔试中都可能·遇得到,所以需要多加练习,熟练掌握。