7.1文件查找之find命令详解

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

    文件查找使用工具: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.shlinux


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

            特色:速度慢数据库

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

    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 "文件名称":查找时忽略字符大小写vim

        -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的bash

        -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/.bashrcless

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

            -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命令时,到底是一次获取全部的参数,仍是
分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来肯定。

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


练习:
一、查找/var/目录属主为root且属组为mail的全部文件;
# find /var -user root -a -group mail

二、查找/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 \)

三、查找/etc/目录下最近一周内其内容修改过的,且不属于root且不属于hadoop的文件;
find /etc -mtime -7 -a -not \(-user root -o -user hadoop\)

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

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

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

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

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

相关文章
相关标签/搜索