zoukankan      html  css  js  c++  java
  • linux 文件查找 find命令详解

    一,从索引库查找文件:locate

    • 索引库:操作系统会周期性的遍历根文件系统,然后生成索引库
    • 手动更新索引库:updatedb
    • 语法:locate [OPTION]... PATTERN...
    • 只匹配basename:-b
    • 统计出有多少个符合条件的文件:-c
    • 使用基本正则表达式:-r
    • 注意:构筑索引,需要遍历整个根文件系统,非常消耗资源。

    二,直接从文件系统里查找:find

    下面写道的【文件】,包含文件和文件夹

    跟locate比,find是精确,实时查找,速度没有locate快。

    • 用法:find [options] [查找起始路径] [查找方式] [查找完,要进行去处理的动作]

    • 查找起始路径:指定搜索目标的起始路径。不指定则为当前目录。

    • 查找方式:指定查找的选项和方式。可以根据文件名,文件大小,文件类型,从属关系,mode等。

      不指定查找方式则查找指定目录下的所有文件。

    • 对查找出来的文件,做出具体的操作,例如删除等。默认操作是把查找结果输出到标准输出。

    **options : **

    • 不查找子目录:-maxdepth 1

    **查找方式: **

    • 按文件名字查找:-name(区分文件名的大小写);-iname(不区分文件名的大小写)

      -name/-iname pattern

      pattern:支持glob语法,但不支持正则表达式。

      # ls test/
      Passwd  Passwd.txt
      # ls passwd
      passwd
      # find ./ -name passwd
      ./passwd
      # find ./ -iname passwd
      ./passwd
      ./test/Passwd
      Passwd.txt不是精确匹配,所以不符合查找条件。
      
      # find ./ -iname "passwd*"
      ./passwd
      ./test/Passwd
      ./test/Passwd.txt
      由于使用了glob通配,Passwd.txt就符合查找条件了。
      
      # find ./ -iname "npasswd?"
      ./test/npasswdo
      ./test/npasswd-
      # find ./ -iname "npasswd[^[:alpha:]]"
      ./test/npasswd-
      # find ./ -iname "npasswd[[:alpha:]]"
      ./test/npasswdo
      
    • 按文件的属主查找:

      -user name/uid

      -uid uid

      # find /tmp/ -user za1
      /tmp/f1
      # id za1
      uid=1001(za1) gid=1001(za1) groups=1001(za1),1000(ys),1002(zg1),1004(gentoo)
      # find /tmp/ -user 1001
      /tmp/f1
      # find /tmp/ -uid 1001
      /tmp/f1
      
    • 按文件的属组查找:

      -group name/gid

      -gid gid

      # find /tmp/ -group zg1
      /tmp/f1
      /tmp/d1/inittab
      # find /tmp/ -group 1002
      /tmp/f1
      /tmp/d1/inittab
      # find /tmp/ -gid 1002
      /tmp/f1
      /tmp/d1/inittab
      
    • 查找g属组被删除了的文件

      # find /tmp/ -nogroup
      /tmp/f1
      /tmp/d1/inittab
      /tmp/d1/issue
      /tmp/fld
      /tmp/test
      # ll -d f1 fld test
      -rw-r-----. 1 1001 1002 511 Dec 18 14:38 f1
      drwxrwxr-x. 2 root 1002   6 Dec 17 22:39 fld
      drwxrwxr-x. 2 ys   1002  85 Dec 23 21:16 test
      # ll d1
      -rwxr-xr--. 1 gentoo 1002 511 Dec 18 14:43 inittab
      -rwxr-xr--. 1 gentoo 1002  23 Dec 18 14:43 issue
      
    • 查找属主被删除了的文件

      # find /tmp/ -nouser
      /tmp/f1
      [root@localhost tmp]# ll f1
      -rw-r-----. 1 1001 1002 511 Dec 18 14:38 f1
      
    • 没有属主或属组的文件非常危险,所以要定期查看这些文件,并把这些文件的属主和属组分配出去,一般分给root。

    • 根据文件的类型查找:-type TYPE

      • f:普通文件

      • d:目录文件

      • l:符号链接文件

        # find /etc/ -type l
        
      • b:块设备文件

        # find /dev -type b -ls
         20033    0 brw-rw----   1 root     disk     253,   2 Dec 23 08:55 /dev/dm-2
         12288    0 brw-rw----   1 root     disk     253,   1 Dec 23 08:55 /dev/dm-1
        
      • c:字符设备文件

      • p:管道文件

      • s:套接字文件

    • 组合查找:可以组合任意的查找方式,比如-name和-size等

      • 与:-a 不指定默认就是-a。
      • 或:-o
      • 非:-not 或者!

      练习1:找出/tmp目录下属主为非root,且文件名不包含fstab的文件。

      注意:括号的用法,括号必须转义,且括号两侧要有空格。

      # find /tmp/ -not -user "root" -not -name "*fstab*"  -ls | wc -l
      98
      # find /tmp/ -not -user "root" -a -not -name "*fstab*"  -ls | wc -l
      98
      # find /tmp/ -not ( -user "root" -o -name "*fstab*" )  -ls | wc -l
      98
      
    • 根据文件大小查找:-size

      • 用法:`-size [+|-]#单位
      • #代表数字(正数)
      • 常用单位:K,M,G
      • #UNIT:(#-1, #]
      • -#UNIT:[0, #-1]
      • +#UNIT:(#, 正无穷)
    • 根据时间查找

      • 以“天”为单位

        -atime [+|-]#(#为数字(正数)):Access time

        • #:[#,#+1)

          例如,#=1,那么就是查找[1,2)天前访问过的文件

          # date
          Tue Dec 24 09:49:21 CST 2019
          # stat /tmp/atime2
          Access: 2019-12-22 08:35:00.000000000 +0800
          # find /tmp/ -atime 1
          #查找不到/tmp/atime2文件,因为是大于2天没访问了
          # touch -at 1912230800.00 /tmp/atime2
          # stat /tmp/atime2
          Access: 2019-12-23 08:00:00.000000000 +0800
          # find /tmp/ -atime 1
          /tmp/atime2 #大于1天并小于2天没访问了,所以找到了。
          # touch -at 1912231000.00 /tmp/atime2
          Access: 2019-12-23 10:00:00.000000000 +0800
          # find /tmp/ -atime 1
          #查找不到/tmp/atime2文件,因为小于1天没有访问。
          
        • +#:[#+1,正无穷)

          例如,#=1,那么就是查找[2,正无穷)天前访问过的文件

          # date
          Tue Dec 24 10:03:57 CST 2019
          # stat /tmp/atime2
          Access: 2019-12-22 11:00:00.000000000 +0800
          # find /tmp/ -atime +1 | grep "atime"
          #查找不到/tmp/atime2文件,因为小于2天没有访问。
          # touch -at 1912221000.00 /tmp/atime2
          # stat /tmp/atime2
          Access: 2019-12-22 10:00:00.000000000 +0800
          # find /tmp/ -atime +1 | grep "atime"
          /tmp/atime2#大于2天没访问了,所以找到了。
          
        • -#:[0, #)

          例如,#=1,那么就是查找[0,1)天前(24小时内)访问过的文件

          # date
          Tue Dec 24 10:13:55 CST 2019
          # stat /tmp/atime2
          Access: 2019-12-23 10:12:00.000000000 +0800
          find /tmp/ -atime -1 | grep "atime"
          #查找不到/tmp/atime2文件,因为大于1天没有访问
          # touch -at 1912231020.00 /tmp/atime2
          # stat /tmp/atime2
          Access: 2019-12-23 10:20:00.000000000 +0800
          # find /tmp/ -atime -1 | grep "atime"
          /tmp/atime2#小于1天没访问了,所以找到了。
          

        -mtime:Modify time

        -ctime:Change time

      • 以“分钟”为单位

        -amin [+|-]#(#为数字(正数)):Access time

        -mmin:Modify time

        -cmin:Change time

    • 根据权限查找

      • 语法:`-perm [-|/]mode

        • mode:精确匹配

          # ll
          total 0
          ---x--x--x. 1 root root 0 Dec 24 13:20 a
          -rw-r--r--. 1 root root 0 Dec 24 13:20 b
          -rw-r--r--. 1 root root 0 Dec 24 13:20 c
          -rw-r--r--. 1 root root 0 Dec 24 13:20 d
          -rw-r--r--. 1 root root 0 Dec 24 13:20 e
          # find ./ -perm 111 -ls
          1692196    0 ---x--x--x   1 root     root            0 Dec 24 13:20 ./a
          # find ./ ! -perm  111 -ls
          1692200    0 drwxrwxr-x   2 ys       1002           69 Dec 24 13:20 ./
          1692205    0 -rw-r--r--   1 root     root            0 Dec 24 13:20 ./b
          1692207    0 -rw-r--r--   1 root     root            0 Dec 24 13:20 ./c
          1969792    0 -rw-r--r--   1 root     root            0 Dec 24 13:20 ./d
          1754172    0 -rw-r--r--   1 root     root            0 Dec 24 13:20 ./e
          
        • /mode:user || group || other

        • -mode:user && group && other

          # ls -al
          total 12
          drwxrwxr-x.  2 ys   1002   33 Dec 24 13:40 .
          drwxrwxrwt. 51 root root 8192 Dec 24 13:20 ..
          ---x--x--x.  1 root root    0 Dec 24 13:20 a
          -rwxrw-r--.  1 root root    0 Dec 24 13:20 b
          -rw-r--r--.  1 root root    0 Dec 24 13:20 c
          属主有执行权限或者属组有写权限的是查找对象
          # find ./  -perm  /120 -ls
          1692200    0 drwxrwxr-x   2 ys       1002           33 Dec 24 13:40 ./
          1692196    0 ---x--x--x   1 root     root            0 Dec 24 13:20 ./a
          1692205    0 -rwxrw-r--   1 root     root            0 Dec 24 13:20 ./b
          属主没有执行权限并且属组没有写权限的是查找对象
          # find ./ ! -perm  /120 -ls
          1692207    0 -rw-r--r--   1 root     root            0 Dec 24 13:20 ./c
          属主有执行权限并且属组有写权限的是查找对象
          # find ./  -perm  -120 -ls
          1692200    0 drwxrwxr-x   2 ys       1002           33 Dec 24 13:40 ./
          1692205    0 -rwxrw-r--   1 root     root            0 Dec 24 13:20 ./b
          属主没有执行权限或者属组没有写权限的是查找对象
          # find ./  ! -perm  -120 -ls
          1692196    0 ---x--x--x   1 root     root            0 Dec 24 13:20 ./a
          1692207    0 -rw-r--r--   1 root     root            0 Dec 24 13:20 ./c
          
    • 查找完,要进行去处理的动作

      -print:把查找结果输出到标准输出。默认的处理动作。

      -ls:类似于对查找到文件执行ls -l命令,把结果输出到标准输出。

      -fls /PATH/NAME:类似于对查找到文件执行ls -l命令,把结果输出指定文件中。

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

      -ok command {} ;:对查找到的每个文件执行command命令,每次操作都需要用户确认。{}就是find查找出来的文件的文件名

      -exec command {} ;:对查找到的每个文件执行command命令,不需要用户确认。

      注意:find是先查找完,然后把查找到的结果一次性传递给后面的命令;而不是查到一个传递一个。但是有些命令不能接受过长的参数,所以后面的命令就会执行失败,使用另一种方式可以解决此问题。

      find | xargs command

      xargs会把find的结果进行分块,就保证了后面的命令不会崩溃 。

      # find ./  ! -perm  -120
      ./a
      ./c
      [root@localhost test]# find ./  ! -perm  -120 -ok cp {} {}.back ;
      < cp ... ./a > ? y
      < cp ... ./c > ? y
      [root@localhost test]# ll
      total 0
      ---x--x--x. 1 root root 0 Dec 24 13:20 a
      ---x--x--x. 1 root root 0 Dec 24 14:40 a.back
      -rwxrw-r--. 1 root root 0 Dec 24 13:20 b
      -rw-r--r--. 1 root root 0 Dec 24 13:20 c
      -rw-r--r--. 1 root root 0 Dec 24 14:40 c.back
      # rm -f ./*.back
      [root@localhost test]# find ./  ! -perm  -120 -exec cp {} {}.back ;
      [root@localhost test]# ll
      total 0
      ---x--x--x. 1 root root 0 Dec 24 13:20 a
      ---x--x--x. 1 root root 0 Dec 24 14:41 a.back
      -rwxrw-r--. 1 root root 0 Dec 24 13:20 b
      -rw-r--r--. 1 root root 0 Dec 24 13:20 c
      -rw-r--r--. 1 root root 0 Dec 24 14:41 c.back
      

    **练习: **

    1,查找/var目录下属主为root,且属组为mail的所有文件或目录。

    # find /var -user root -group mail
    /var/spool/mail
    /var/spool/mail/root
    # find /var -user root -group mail | xargs ls -ld
    drwxrwxr-x. 2 root mail    167 Dec 23 16:42 /var/spool/mail
    -rw-------. 1 root mail 463001 Dec 17 20:59 /var/spool/mail/root
    

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

    # find /usr ! -user root ! -user bin ! -user gentoo | wc -l
    14
    # find /usr ! ( -user root -o -user bin -o -user gentoo ) | wc -l
    14
    

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

    # find /tmp -mtime -7 ! -user root ! -user gentoo
    

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

    #  find /tmp ( -nouser -o -nogroup ) -atime -7 -ls
    67978820    4 -rw-r-----   1 1001     1002          511 Dec 18 14:38 /tmp/f1
    67978822    4 -rwxr-xr--   1 gentoo   1002          511 Dec 18 14:43 /tmp/d1/inittab
    67978823    4 -rwxr-xr--   1 gentoo   1002           23 Dec 18 14:43 /tmp/d1/issue
    33599012    0 drwxrwxr-x   2 root     1002            6 Dec 17 22:39 /tmp/fld
    1692200     0 drwxrwxr-x   2 ys       1002           33 Dec 24 14:43 /tmp/test
    

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

    # find /tmp -size +1M -type f | xargs ls -lh
    -rw--w----. 1 root mageedu 2.3M Dec 18 10:44 /tmp/log/anaconda/journal.log
    -rw--w----. 1 root mageedu 1.8M Dec 18 10:44 /tmp/log/audit/audit.log
    -r---w----. 1 root mageedu 8.1M Dec 18 10:44 /tmp/log/audit/audit.log.1
    -r---w----. 1 root mageedu 8.1M Dec 18 10:44 /tmp/log/audit/audit.log.2
    -r---w----. 1 root mageedu 8.1M Dec 18 10:44 /tmp/log/audit/audit.log.3
    -rw-rw-r--. 1 root mageedu 1.6M Dec 18 10:44 /tmp/log/firewalld
    -rw-rw-r--. 1 root mageedu 1.2M Dec 18 10:44 /tmp/log/lastlog
    -rw--w----. 1 root mageedu 1.1M Dec 18 10:44 /tmp/log/messages
    -rw--w----. 1 root mageedu 4.4M Dec 18 10:44 /tmp/log/messages-20191206
    -rw--w----. 1 root mageedu  49M Dec 18 10:44 /tmp/log/messages-20191215
    

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

    # find /etc ! -perm /222 -type f | xargs ls -l
    -r--r--r--. 1 root root     460 Apr 11  2018 /etc/dbus-1/system.d/cups.conf
    ----------. 1 root root     920 Dec 23 21:38 /etc/gshadow
    ----------. 1 root root     927 Dec 23 21:33 /etc/gshadow-
    -r--r--r--. 1 root root      63 Nov  9  2018 /etc/ld.so.conf.d/kernel-3.10.0-957.el7.x86_64.conf
    

    7,查找某个目录目录,至少有一类用户没有执行权限的文件

    # ll
    -rwxr-xr-x. 1 root root 0 Dec 24 15:45 a
    -rwxr--r--. 1 root root 0 Dec 24 15:45 b
    -rw-r-xr--. 1 root root 0 Dec 24 15:45 c
    -rw-r-xr-x. 1 root root 0 Dec 24 15:45 d
    [root@localhost test1]# find ./ ! -perm -111 -type f | xargs ls -l
    -rwxr--r--. 1 root root 0 Dec 24 15:45 ./b
    -rw-r-xr--. 1 root root 0 Dec 24 15:45 ./c
    -rw-r-xr-x. 1 root root 0 Dec 24 15:45 ./d
    

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

    # ll /etc/init.d/
    total 40
    -rw-r--r--. 1 root root 18281 Aug 24  2018 functions
    -rwxr-xrwx. 1 root root  4569 Aug 24  2018 netconsole
    -rwxr-xr-x. 1 root root  7923 Aug 24  2018 network
    -rw-r--r--. 1 root root  1160 Oct 31  2018 README
    # find /etc/init.d/ -perm -111 -perm /002 -type f -ls
    101249165    8 -rwxr-xrwx   1 root     root         4569 Aug 24  2018 /etc/init.d/netconsole
    # find /etc/init.d/ -perm -113 -type f -ls
    101249165    8 -rwxr-xrwx   1 root     root         4569 Aug 24  2018 /etc/init.d/netconsole
    
    # c/c++ 学习互助QQ群:877684253 ![](https://img2018.cnblogs.com/blog/1414315/201811/1414315-20181106214320230-961379709.jpg) # 本人微信:xiaoshitou5854
  • 相关阅读:
    [音乐欣赏]为你读诗背景音乐,音乐电台
    [读书笔记]1368个单词就够了
    [读书笔记]项目管理实战:Microsoft Project精髓与方法
    [音乐欣赏]动力火车 艾琳娜
    [办公自动化]PDF大小不一如何调整
    [读书笔记]左手数据,右手图表
    [写作新思路]数据分析
    揪出Android流氓软件
    [办公自动化]Wlan无法启动,无法连接无线网wifi,所有无线网都搜索不到
    日常UVA题目英语积累
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/12092244.html
Copyright © 2011-2022 走看看