7.1文件查找之find命令详解

文件查找:根据文件的各种属性去找到相对应文件
   
    文本搜索:grep, egrep, fgrep   根据文本内容进行字符匹配查找

    文件查找使用工具:locate, find
    [root@root scripts]#type locate
    locate is /usr/bin/locate
    [root@root scripts]#locate --help
    Usage: locate [OPTION]... [PATTERN]...
    Search for entries in a mlocate database.
        实时查找:遍历所有文件进行条件匹配
        非实时查找:根据索引查找
        [root@root scripts]#man whatis
        NAME
               whatis - search the whatis database for complete words.
                         在whatis数据库上搜索完整的单词
        SYNOPSIS
               whatis keyword ...
       
        DESCRIPTION
               whatis  searches a set of database files containing short descriptions of system com-
               mands for keywords and displays the result on the  standard  output.   Only  complete
               word matches are displayed.
       
               The whatis database is created using the command /usr/sbin/makewhatis.
               使用命令/usr/sbin/makewhatis 创建whatis数据库。
        [root@root scripts]#whatis ls   whatis查找也是需要数据库的,是通过makewhatis来创建数据库的
        ls                   (1)  - list directory contents
        ls                   (1p)  - list directory contents
       
        locate: 非实时查找工具
            特点:需要依赖于索引,而索引构建相当占用资源;索引的创建是在系统空闲时由系统自动进行(每天任务);手动
            进行使用updatedb命令来创建数据库;               
                查找速度快
                非精准查找   索引的更新不是实时的
            模糊查找  在基本意义上的匹配查找
            [root@root blankdir]#locate /etc/p*
            。。。。。。。。。。。。。。
            /etc/profile.d/colorls.csh
            /etc/profile.d/colorls.sh
            /etc/profile.d/glib2.csh
            /etc/profile.d/glib2.sh
            /etc/profile.d/lang.csh
            /etc/profile.d/lang.sh
            /etc/profile.d/less.csh
            /etc/profile.d/less.sh
            /etc/profile.d/vim.csh
            /etc/profile.d/vim.sh
            /etc/profile.d/which2.sh


        find: 实时查找工具,通过遍历文件来匹配  
            精准查找
            精确查找

            特点:速度慢

    find [option]... [查找路径] [查找条件] [处理动作]      隐藏文件也会查找到
    find - search for files in a directory hierarchy

    SYNOPSIS
           find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
   
    DESCRIPTION
           This  manual page documents the GNU version of find.  GNU find searches the directory
           tree rooted at each given file name by evaluating the given expression from  left  to
           right,  according  to the rules of precedence (see section OPERATORS), until the out-
           come is known (the left hand side is false for and operations, true for or), at which
           point find moves on to the next file name.
        查找路径:默认为当前目录
        查找条件:默认为指定路径下的所有文件
        处理动作:默认为显示至屏幕
        -ls    True;  list  current  file  in  ls -dils format on standard output.
                        列出当前文件像ls -dils格式到标准输出。因为下面没有指定查找
                        条件,所以会查找所有文件
  [root@root scripts]#find /var/log/ -ls(不是选项,是一个指定的处理动作)
    524290    4 drwxr-xr-x  10 root     root         4096 Dec 28 03:22 /var/log/
    524685    0 -rw-------   1 root     root            0 Dec 28 03:22 /var/log/maillog
    524303    0 -rw-------   1 root     root            0 Dec  5 23:39 /var/log/tallylog
    。。。。。。。。。。。。。。。。。。
   
    最核心的是查找条件:
        -name "文件名称":支持使用globbing字符   最常用到的  默认查找是区分字符大小写的
            *:  任意长度的任意字符
            ?:  任意单个字符
            []: 指定范围的任意单个字符
            [^]: 指定范围外的任意单个字符  通配符上的元字符
            [!]:指定范围外的任意单个字符  
            这里 指定范围外的任意单个字符 都可以使用,但是只能在两边使用,
            在中间使用时,是无效的。并且在中间的  指定范围任意单个字符  都是无效的
-----------------------------------------------------
[root@root blankdir]#ls           
fstab  issue  rc.sysinit
[root@root blankdir]#find ./ -name "[??s*]"
[root@root blankdir]#find ./ -name "*[!re]*"
./
./rc.sysinit
./fstab
./issue
[root@root blankdir]#find ./ -name "*[^re]*"
./
./rc.sysinit
./fstab
./issue
[root@root blankdir]#find ./ -name "[!rc]*"
./
./fstab
./issue
[root@root blankdir]#find ./ -name "[^rc]*"
./
./fstab
./issue
[root@root blankdir]#find ./ -name "[^rf]*"
./
./issue
[root@root blankdir]#touch hello
[root@root blankdir]#find ./ -name "*[!s]*"
./
./rc.sysinit
./hello
./fstab
./issue
[root@root blankdir]#find ./ -name "*[^s]*"
./
./rc.sysinit
./hello
./fstab
./issue
[root@root blankdir]#find ./ -name "[^hfi]*"
./
./rc.sysinit
[root@root blankdir]#find ./ -name "[!hfi]*"
./
./rc.sysinit
[root@root blankdir]#ls
fstab  hello  issue  rc.sysinit
[root@root blankdir]#find ./ -name "*!eo"
-bash: !eo": event not found
[root@root blankdir]#find ./ -name "*[!eo]"
./
./rc.sysinit
./fstab
[root@root blankdir]#find ./ -name "*[^eo]"
./
./rc.sysinit
./fstab
[root@root blankdir]#touch h!ol
-bash: !ol: event not found
[root@root blankdir]#touch h\!ol
[root@root blankdir]#ls
fstab  hello  h!ol  issue  rc.sysinit
[root@root blankdir]#touch o^ve
[root@root blankdir]#ls
fstab  hello  h!ol  issue  o^ve  rc.sysinit
[root@root blankdir]#find ./ -name "*[e!]*"
-bash: !]*": event not found
[root@root blankdir]#find ./ -name "*[!e]*"  因为非e后面还跟了*,*是指任意多个任意字符,所以即使前面把e取非后面还是可以包含进去的
./
./rc.sysinit
./hello
./o^ve
./h!ol
./fstab
./issue
[root@root blankdir]#find ./ -name "*[^e]*"
./
./rc.sysinit
./hello
./o^ve
./h!ol
./fstab
./issue
[root@root blankdir]#ls
fstab  hello  h!ol  issue  o^ve  rc.sysinit
[root@root blankdir]#find ./ -name "*[vu]*"
./o^ve
./issue
[root@root blankdir]#find ./ -name "*[\!]*"
./h!ol
-----------------------------------------------------           
*****************************************************
find命令在指定字串的中间是取非是有条件的:你必须得知道那个字串的长度,根据指定个数的字符来做匹配就行
[root@root blankdir]#ls hel
heleo  hello  heloo 
[root@root blankdir]#find ./ -name "???[^e]o"
./hello
./heloo
[root@root blankdir]#find ./ -name "???[^el]o"
./heloo
*******************************************************           
            查找以p开头的所有文件
        [root@root ~]#find /etc/ -name "p*"
        /etc/prelink.cache
        /etc/openldap/certs/password   
     。。。。。。。。。。。。。。。
     查找以p开头的所有文件,且有一位数字的文件
     [root@root ~]#find /etc/ -name "p*[0-9]*"
        /etc/selinux/targeted/policy/policy.24
        /etc/pki/nssdb/pkcs11.txt
        /etc/polkit-1
        。。。。。
        -iname "文件名称":查找时忽略字符大小写

        -user USERNAME: 根据文件的属主查找
        -group GRPNAME: 根据文件的属组查找
   [root@root ~]#find /home/ -user root
        /home/
        /home/me
        /home/you
        /home/mageedu
        /home/mageedu/.bash_profile
        /home/mageedu/.bash_logout
        /home/mageedu/.bashrc
        [root@root ~]#find /home/ -group root
        /home/
        /home/me
        /home/you
        /home/mageedu
        /home/mageedu/.bash_profile
        /home/mageedu/.bash_logout
        /home/mageedu/.bashrc
        [root@root ~]#find /home/ -group root -ls
        524293    4 drwxr-xr-x  39 root     root         4096 Dec 28 13:26 /home/
        525530    4 drwxr-xr-x   2 root     root         4096 Dec 22 14:24 /home/me
        525529    4 drwxr-xr-x   2 root     root         4096 Dec 22 14:21 /home/you
        525564    4 drwx------   2 root     root         4096 Dec 22 16:44 /home/mageedu
        525565    4 -rw-------   1 root     root          176 Dec 22 16:44 /home/mageedu/.bash_profile
        525566    4 -rw-------   1 root     root           18 Dec 22 16:44 /home/mageedu/.bash_logout
        525567    4 -rw-------   1 root     root          124 Dec 22 16:44 /home/mageedu/.bashrc
chmod 任何用户都可以使用,chown,chgrp只有管理员才能使用的
        -uid UID
        -gid GID
        [root@root blankdir]#find /home/ -uid 1025 -ls
        525655    4 drwx------   2 1025     1029         4096 Dec 28 13:25 /home/user601
        525656    4 -rw-r--r--   1 1025     1029          176 Oct 16 21:56 /home/user601/.bash_profile
        525657    4 -rw-r--r--   1 1025     1029           18 Oct 16 21:56 /home/user601/.bash_logout
        没有删除家目录,文件格式中是显示其uid和gid的

        -nouser: 查找没有属主的文件
        -nogroup: 查找没有属组的文件
    [root@root ~]#userdel user5
        [root@root ~]#find /home/ -nouser -ls
        525655    4 drwx------   2 1025     1029         4096 Dec 28 13:25 /home/user601
        525656    4 -rw-r--r--   1 1025     1029          176 Oct 16 21:56 /home/user601/.bash_profile
        525657    4 -rw-r--r--   1 1025     1029           18 Oct 16 21:56 /home/user601/.bash_logout
        525658    4 -rw-r--r--   1 1025     1029          124 Oct 16 21:56 /home/user601/.bashrc
        525540    4 drwx------   2 1002     1003         4096 Dec 21 17:45 /home/user5
        525541    4 -rw-r--r--   1 1002     1003          176 Oct 16 21:56 /home/user5/.bash_profile
        525542    4 -rw-r--r--   1 1002     1003           18 Oct 16 21:56 /home/user5/.bash_logout
        525543    4 -rw-r--r--   1 1002     1003          124 Oct 16 21:56 /home/user5/.bashrc

        组合条件查找:
            与:-a, 同时满足
            或:-o, 满足一个即可   两边要同时判断再取或   就像数学中的集合概念
            非:-not, !,条件取反

            -not A -a -not B = -not (A -o B)  -a是可以省略的,默认为与
            -not A -o -not B = -not (A -a B)

                例子:-not \( -iname "*r* -o -user gentoo \)
                查找/etc/目录下的以p开头且用户为root的文件
            [root@root ~]#find /etc -name "p*" -a -user root -ls  -a可以不用写的
            131111  128 -rw-r--r--   1 root     root       128669 Dec 23 03:15 /etc/prelink.cache
            133083    4 -r--------   1 root     root           45 Dec  8 20:06 /etc/openldap/certs/password
            135584    4 -rwxr-xr-x   1 root     root         3912 Feb 20  2014 /etc/rc.d/init.d/postfix
            136429    4 -rwxr-xr-x   1 root     root         1556 Jul 17  2012 /etc/rc.d/init.d/psacct
      查找/home下不是root用户的文件
      find /home -not -user root -ls
    
     -ls在如下情况会和想像的显示结果不同    注意注意!!!::::::
     [root@root blankdir]#ls -l
            total 28
            -rw-r--r--. 1 root root   863 Dec 29 20:55 fstab
            -rw-r--r--. 1 root root    47 Dec 29 20:55 issue
            -rwxr-xr-x. 1 root root 19914 Dec 29 20:55 rc.sysinit
            [root@root blankdir]#id user
            uid=510(user) gid=1009(hello) groups=1009(hello),1012(me)
            [root@root blankdir]#chown user. issue  默认修改属组为hello            
            [root@root blankdir]#ls -l issue
            -rw-r--r--. 1 user hello 47 Dec 29 20:55 issue
            ------------------------------------------------------
            [root@root blankdir]#chown root. issue   属主和属组都会改变
            [root@root blankdir]#ls -l issue
            -rw-r--r--. 1 root root 47 Dec 29 20:55 issue
            [root@root blankdir]#chown user issue   用来修改属主
            [root@root blankdir]#ls -l issue
            -rw-r--r--. 1 user root 47 Dec 29 20:55 issue
            ------------------------------------------------------
            查找含有s且属主为root用户的文件
            [root@root blankdir]#find ./ -iname "*s*" -user root -ls
            401968   20 -rwxr-xr-x   1 root     root        19914 Dec 29 20:55 ./rc.sysinit
            401983    4 -rw-r--r--   1 root     root          863 Dec 29 20:55 ./fstab
            查找含有s且属主为user用户的文件
            [root@root blankdir]#find ./ -iname "*s*" -user user -ls
      401984    4 -rw-r--r--   1 user     root           47 Dec 29 20:55 ./issue
      查找含有r且用户不是user的文件
      [root@root blankdir]#find ./ -iname "*r*" -a -not -user user -ls
      401968   20 -rwxr-xr-x   1 root     root        19914 Dec 29 20:55 ./rc.sysinit
      查找不含有r且用户不是user的文件
      [root@root blankdir]#find ./ -not -iname "*r*" -a -not -user user -ls
            399572    4 drwxr-xr-x   2 root     root         4096 Dec 29 20:55 ./
            401983    4 -rw-r--r--   1 root     root          863 Dec 29 20:55 ./fstab
            查找不含有r或者用户不是user的文件
         [root@root blankdir]#find ./ -not -iname "*r*" -o -not -user user -ls 此时-ls出乱了
            401968   20 -rwxr-xr-x   1 root     root        19914 Dec 29 20:55 ./rc.sysinit
            [root@root blankdir]#find ./ -not -iname "*r*" -o -not -user user
            ./
            ./rc.sysinit
            ./fstab
            ./issue
            [root@root blankdir]#find ./ -not \( -iname "*r*" -a  -user user \)  ()两边都需要有空格
            ./
            ./rc.sysinit
            ./fstab
            ./issue
            [root@root blankdir]#find ./ -iname "*r*" -o -not -user user
            ./
            ./rc.sysinit
            ./fstab
            [root@root blankdir]#find ./ -iname "*r*" -o -not -user user -ls
            399572    4 drwxr-xr-x   2 root     root         4096 Dec 29 20:55 ./
            401983    4 -rw-r--r--   1 root     root          863 Dec 29 20:55 ./fstab
           
                
        -type TYPE: 根据文件类型查找
            f: 普通文件
            d: 目录文件
            l: 符号链接
            b: 块设备
            c: 字符设备
            s: 套接字文件
            p: 命名管道
        [root@root blankdir]#find /home/ -type d 查找/home目录下的目录
        /home/
        /home/user602
        /home/how
        /home/tmpuser7
        /home/user609
        。。。。。。。。。。。。。

        -size [+|-]#UNIT
            常用单位: k, M, G

            #UNIT: #-1 < x <= #   大于#-1小于#
            -#UNIT: x <= #-1    小于等于#-1
            +#UNIT: x > #     大于#的
     [root@root blankdir]#find /var/log/ -size 2k
            /var/log/boot.log
            /var/log/httpd/port_log-20141221
            /var/log/httpd/access_log-20141221
            /var/log/maillog-20141207
            /var/log/mysqld.log
            -rw-------. 1 root  root  1.9K Dec  7 02:15 maillog-20141207
      -rw-r-----. 1 mysql mysql 1.7K Dec 20 12:20 mysqld.log
        根据时间戳查找:
        有两种方式
            以“天”为单位
                -atime [+|-]#
                    +#:x >= #+1  在#+1之前被访问过的文件
                    -#:x < #
                    #: # <= x < #+1
                -mtime
                -ctime

            以“分钟”为单位
                -amin
                -mmin
                -cmin

        根据权限查找:
            -perm [+|-]MODE
                MODE: 与MODE精确匹配
                    find ./ -perm 644
                +MODE: 任何一类用户的权限只要能包含对其指定的任何一位权限即可;以属主为例,
                    find ./ -perm +222    任何一类用户有写权限的  常用+001  +002
                -MODE:每类用户指定的检查权限都匹配(可以是包含关系):   
                    为三类用户所有指定的检查权限都能够被包含
                    find ./ -perm -222

    处理动作:
        -print: 默认处理动作,显示
        -ls:类似于ls -dils
        -exec COMMAND {} \;  对查找到的文件进行操作  对找到的指定权限的文件修改权限 {}是用来引用文件本身的
        -ok COMMAND {} \;  每一个文件在操作之前都需要确认

        find: 一次性查找符合条件的所有文件,并一同传递给给-exec或-ok后面指定的命令;但,有些命令不能接受过
        长的参数(文件查找到很多后,处理时会出现问题);此时使用另一种方式

            对查找到很多的文件进行处理 find | xargs COMMAND
           
     xargs - build and execute command lines from standard input
    
    总结:find [查找路径] [查找条件] [处理动作]
        查找条件:
            -name, -iname, -user, -group, -uid, -gid, -nouser, -nogroup, -type, -size, -atime, -mtime, -ctime, -amin, -mmin, -cmin, -perm
            组合:-a, -o, -not
        处理动作:
*************************************************************************************
find补充材料(摘自互联网):


find与xargs
在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec
执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出
现 溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别
是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不
像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到
的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而
效率不高;

而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是
分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

*************************************************************************************


练习:
1、查找/var/目录属主为root且属组为mail的所有文件;
# find /var -user root -a -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 -o -user hadoop\)

4、查找当前系统上没有属主或属组,且最近1个月内曾被访问过的文件;
find / \(-nouser -o -nogroup\) -a -atime -30   优先级是 非  与   或

5、查找/etc/目录下大于1M且类型为普通文件的所有文件;
find /etc -size +1M -type f

6、查找/etc/目录所有用户都没有写权限的文件;
find /etc/ -not -perm +222

7、查找/etc/目录下至少有一类用户没有写权限;
find /etc/ -not -perm -222

8、查找/etc/init.d/目录下,所有用户都有执行权限且其它用户有写权限的文件;
find /etc/init.d/ -perm -113

转载于:https://my.oschina.net/u/1762991/blog/664796

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值