一,从索引库查找文件: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语法,但不支持正则表达式。

    1. # ls test/
    2. Passwd Passwd.txt
    3. # ls passwd
    4. passwd
    5. # find ./ -name passwd
    6. ./passwd
    7. # find ./ -iname passwd
    8. ./passwd
    9. ./test/Passwd
    10. Passwd.txt不是精确匹配,所以不符合查找条件。
    11. # find ./ -iname "passwd*"
    12. ./passwd
    13. ./test/Passwd
    14. ./test/Passwd.txt
    15. 由于使用了glob通配,Passwd.txt就符合查找条件了。
    16. # find ./ -iname "npasswd?"
    17. ./test/npasswdo
    18. ./test/npasswd-
    19. # find ./ -iname "npasswd[^[:alpha:]]"
    20. ./test/npasswd-
    21. # find ./ -iname "npasswd[[:alpha:]]"
    22. ./test/npasswdo
  • 按文件的属主查找:

    -user name/uid

    -uid uid

    1. # find /tmp/ -user za1
    2. /tmp/f1
    3. # id za1
    4. uid=1001(za1) gid=1001(za1) groups=1001(za1),1000(ys),1002(zg1),1004(gentoo)
    5. # find /tmp/ -user 1001
    6. /tmp/f1
    7. # find /tmp/ -uid 1001
    8. /tmp/f1
  • 按文件的属组查找:

    -group name/gid

    -gid gid

    1. # find /tmp/ -group zg1
    2. /tmp/f1
    3. /tmp/d1/inittab
    4. # find /tmp/ -group 1002
    5. /tmp/f1
    6. /tmp/d1/inittab
    7. # find /tmp/ -gid 1002
    8. /tmp/f1
    9. /tmp/d1/inittab
  • 查找g属组被删除了的文件

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

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

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

    • f:普通文件

    • d:目录文件

    • l:符号链接文件

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

      1. # find /dev -type b -ls
      2. 20033 0 brw-rw---- 1 root disk 253, 2 Dec 23 08:55 /dev/dm-2
      3. 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的文件。

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

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

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

    • 以“天”为单位

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

      • #:[#,#+1)

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

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

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

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

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

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

      -mtime:Modify time

      -ctime:Change time

    • 以“分钟”为单位

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

      -mmin:Modify time

      -cmin:Change time

  • 根据权限查找

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

      • mode:精确匹配

        1. # ll
        2. total 0
        3. ---x--x--x. 1 root root 0 Dec 24 13:20 a
        4. -rw-r--r--. 1 root root 0 Dec 24 13:20 b
        5. -rw-r--r--. 1 root root 0 Dec 24 13:20 c
        6. -rw-r--r--. 1 root root 0 Dec 24 13:20 d
        7. -rw-r--r--. 1 root root 0 Dec 24 13:20 e
        8. # find ./ -perm 111 -ls
        9. 1692196 0 ---x--x--x 1 root root 0 Dec 24 13:20 ./a
        10. # find ./ ! -perm 111 -ls
        11. 1692200 0 drwxrwxr-x 2 ys 1002 69 Dec 24 13:20 ./
        12. 1692205 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./b
        13. 1692207 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./c
        14. 1969792 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./d
        15. 1754172 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./e
      • /mode:user || group || other

      • -mode:user && group && other

        1. # ls -al
        2. total 12
        3. drwxrwxr-x. 2 ys 1002 33 Dec 24 13:40 .
        4. drwxrwxrwt. 51 root root 8192 Dec 24 13:20 ..
        5. ---x--x--x. 1 root root 0 Dec 24 13:20 a
        6. -rwxrw-r--. 1 root root 0 Dec 24 13:20 b
        7. -rw-r--r--. 1 root root 0 Dec 24 13:20 c
        8. 属主有执行权限或者属组有写权限的是查找对象
        9. # find ./ -perm /120 -ls
        10. 1692200 0 drwxrwxr-x 2 ys 1002 33 Dec 24 13:40 ./
        11. 1692196 0 ---x--x--x 1 root root 0 Dec 24 13:20 ./a
        12. 1692205 0 -rwxrw-r-- 1 root root 0 Dec 24 13:20 ./b
        13. 属主没有执行权限并且属组没有写权限的是查找对象
        14. # find ./ ! -perm /120 -ls
        15. 1692207 0 -rw-r--r-- 1 root root 0 Dec 24 13:20 ./c
        16. 属主有执行权限并且属组有写权限的是查找对象
        17. # find ./ -perm -120 -ls
        18. 1692200 0 drwxrwxr-x 2 ys 1002 33 Dec 24 13:40 ./
        19. 1692205 0 -rwxrw-r-- 1 root root 0 Dec 24 13:20 ./b
        20. 属主没有执行权限或者属组没有写权限的是查找对象
        21. # find ./ ! -perm -120 -ls
        22. 1692196 0 ---x--x--x 1 root root 0 Dec 24 13:20 ./a
        23. 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的结果进行分块,就保证了后面的命令不会崩溃 。

    1. # find ./ ! -perm -120
    2. ./a
    3. ./c
    4. [root@localhost test]# find ./ ! -perm -120 -ok cp {} {}.back \;
    5. < cp ... ./a > ? y
    6. < cp ... ./c > ? y
    7. [root@localhost test]# ll
    8. total 0
    9. ---x--x--x. 1 root root 0 Dec 24 13:20 a
    10. ---x--x--x. 1 root root 0 Dec 24 14:40 a.back
    11. -rwxrw-r--. 1 root root 0 Dec 24 13:20 b
    12. -rw-r--r--. 1 root root 0 Dec 24 13:20 c
    13. -rw-r--r--. 1 root root 0 Dec 24 14:40 c.back
    14. # rm -f ./*.back
    15. [root@localhost test]# find ./ ! -perm -120 -exec cp {} {}.back \;
    16. [root@localhost test]# ll
    17. total 0
    18. ---x--x--x. 1 root root 0 Dec 24 13:20 a
    19. ---x--x--x. 1 root root 0 Dec 24 14:41 a.back
    20. -rwxrw-r--. 1 root root 0 Dec 24 13:20 b
    21. -rw-r--r--. 1 root root 0 Dec 24 13:20 c
    22. -rw-r--r--. 1 root root 0 Dec 24 14:41 c.back

**练习: **

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

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

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

  1. # find /usr ! -user root ! -user bin ! -user gentoo | wc -l
  2. 14
  3. # find /usr ! \( -user root -o -user bin -o -user gentoo \) | wc -l
  4. 14

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

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

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

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

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

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

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

  1. # find /etc ! -perm /222 -type f | xargs ls -l
  2. -r--r--r--. 1 root root 460 Apr 11 2018 /etc/dbus-1/system.d/cups.conf
  3. ----------. 1 root root 920 Dec 23 21:38 /etc/gshadow
  4. ----------. 1 root root 927 Dec 23 21:33 /etc/gshadow-
  5. -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,查找某个目录目录,至少有一类用户没有执行权限的文件

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

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

  1. # ll /etc/init.d/
  2. total 40
  3. -rw-r--r--. 1 root root 18281 Aug 24 2018 functions
  4. -rwxr-xrwx. 1 root root 4569 Aug 24 2018 netconsole
  5. -rwxr-xr-x. 1 root root 7923 Aug 24 2018 network
  6. -rw-r--r--. 1 root root 1160 Oct 31 2018 README
  7. # find /etc/init.d/ -perm -111 -perm /002 -type f -ls
  8. 101249165 8 -rwxr-xrwx 1 root root 4569 Aug 24 2018 /etc/init.d/netconsole
  9. # find /etc/init.d/ -perm -113 -type f -ls
  10. 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

linux 文件查找 find命令详解的更多相关文章

  1. Linux中3个文件查找相关命令详解

    源于:https://mp.weixin.qq.com/s/VPs-IXY6RoxbltHIxtIbng which命令 我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令 ...

  2. linux文件编辑VI命令详解

    vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相 ...

  3. [r]Ubuntu Linux系统下apt-get命令详解

    Ubuntu Linux系统下apt-get命令详解(via|via) 常用的APT命令参数: apt-cache search package 搜索包 apt-cache show package ...

  4. Linux文件权限与属性详解 之 SUID、SGID & SBIT

    Linux文件权限与属性详解 之 一般权限 Linux文件权限与属性详解 之 ACL Linux文件权限与属性详解 之 SUID.SGID & SBIT Linux文件权限与属性详解 之 ch ...

  5. Linux上的free命令详解、swap机制

    Linux上的free命令详解   解释一下Linux上free命令的输出. 下面是free的运行结果,一共有4行.为了方便说明,我加上了列号.这样可以把free的输出看成一个二维数组FO(Free ...

  6. Linux CAT与ECHO命令详解 <<EOF EOF

    Linux CAT与ECHO命令详解 cat命令是Linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: .一次显示整个文件. $ cat filename .从键盘 ...

  7. Linux文件权限与属性详解 之 一般权限

    目录 一般属性 1. iNode: 3152621 2. 文件类型 3.文件访问权限 4. 链接数目: 5. 文件所有者 6. 文件所属组 7. 文件大小 8. 修改时间 9. 文件名称 Linux文 ...

  8. Linux文件权限与属性详解 之 ACL

    Linux文件权限与属性详解 之 一般权限 Linux文件权限与属性详解 之 ACL Linux文件权限与属性详解 之 SUID.SGID & SBIT Linux文件权限与属性详解 之 ch ...

  9. Linux文件权限与属性详解 之 chattr & lsattr

    Linux文件权限与属性详解 之 一般权限 Linux文件权限与属性详解 之 ACL Linux文件权限与属性详解 之 SUID.SGID & SBIT Linux文件权限与属性详解 之 ch ...

随机推荐

  1. 解决 /usr/lib/x86_64-linux-gnu/liblapack.so.3: undefined reference to `gotoblas'

    最近需要用到openblas库,本地ubuntu 系统内有一个版本,但是想用自己新编译的openblas, 使用cmake指定了链接的库地址, SET(LINK_LIB_DIRS "/usr ...

  2. 如何获取当前包名与activitity&&抓log

    若hi1:获取当前包名以及Activity (1)adb shell dumpsys activity | find "mFocusedActivity" (2)adb shell ...

  3. c++ 指针(三)

    指针 (6)传递指针给函数 只需要简单地声明函数参数为指针类型即可 #include <iostream> #include <ctime> using namespace s ...

  4. 使用 Blender* 重新拓扑 VR 和游戏素材

    本文介绍如何将网格重新拓扑成一个整洁的低密度模型,然后 UV 解包该网格,以便将纹理贴添加至新模型.本文还将探讨如何使用免费工具,比如 Blender* 及其 Bsurface 插件,重新拓扑雕塑的 ...

  5. cocos2dx基础篇(6) 定时器schedule/update

    定时器在大部分游戏中是不可或缺的,即每隔一段时间,就要执行相应的刷新体函数,以更新游戏的画面.时间.进度.敌人的指令等等.cocos2dx为我们提供了定时器schedule相关的操作.其操作函数的定义 ...

  6. selenium—隐式等待和显式等待

    一.隐式等待和显式等待的区别 隐式等待:是整个页面的等待.设置一个最长的等待时间,在规定时间内整个页面加载完成,则执行下一步,否则继续等待直到最长等待时间结束. 显式等待:是针对某个元素的等待.在设置 ...

  7. python 并发编程 多线程 GIL与Lock

    GIL与Lock Python已经有一个GIL来保证同一时间只能有一个线程来执行了,为什么这里还需要互斥锁lock? 锁的目的是为了保护共享的数据,同一时间只能有一个线程来修改共享的数据 GIT保证了 ...

  8. linux 编程头文件搜索规则

    包含头文件有两种写法,分别是:#include <stdio.h>#include "stdio.h" <>和""分别表示搜索位置的方式 ...

  9. Pikachu漏洞练习平台实验——XSS(二)

    概述 简介 XSS是一种发生在Web前端的漏洞,所以其危害的对象也主要是前端用户 XSS漏洞可以用来进行钓鱼攻击.前端js挖矿.盗取用户cookie,甚至对主机进行远程控制 攻击流程 假设存在漏洞的是 ...

  10. AttributeError: 'dict' object has no attribute 'status_code'

    前端AJAX请求数据,提示错误:“AttributeError: 'dict' object has no attribute 'status_code'”. 原因:是提示返回对象dict没有“sta ...