一,从索引库查找文件: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

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. 详解嵌套ListView、ScrollView布局显示不全的问题

    在项目开发中,可能经常遇到嵌套ListView.ScrollView的问题,就是重写onMeasure方法.解决如下 public class ExpandListView extends ListV ...

  2. Maven POM 模板[z]

    https://juejin.im/post/5cc826a5f265da03a33c443a [z]https://juejin.im/post/5cc826a5f265da03a33c443a S ...

  3. idea 编译 brooklin

    gradle 项目导入 idea 之后,各种报错,run 不起来 手动加入各种依赖 配置启动类 指定 log4j.properties

  4. idea 导入 open项目

    导入  import Project 选择你的项目目录 (位置可以直接是svn下下来的项目目录 不用新建空项目或者目录) 如果有提示直接yes  没有拉到 一路next 如果提示  覆盖就ok (一路 ...

  5. Dart学习笔记-变量常量数据类型

    变量和常量 1.变量的定义 main() { var t_str = 'hello world'; var t_num = 123456; String t_str2 = '你好,我很高兴'; int ...

  6. Word2007—如何快速取消自动编号

    有时候自动编号很麻烦,有没有好的办法可以快速取消呢. 1.禁止自动编号.在Word为其自动加上编号时,只要按下Ctrl+Z键撤销操作,此时自动编号会消失,而且每次键入数字时,该功能就会被禁止了. 2. ...

  7. spring + spring-data-redist + Redis 单机、集群(cluster模式,哨兵模式)

    一.单机redis配置 1. 配置redis连接池 <bean id="jedisPoolConfig" class="redis.clients.jedis.Je ...

  8. Lombok的用法

    Lombok是一款Java开发插件,使得Java开发者可以通过其定义的一些注解来消除业务工程中冗长和繁琐的代码,尤其对于简单的Java模型对象(POJO).在开发环境中使用Lombok插件后,Java ...

  9. [原创]关于类似方程x+y+z=P的解的总解

    1:如果x,y,z>=0,则直接插板法c(P+3,3-1)2:如果x,y,z均有下界a1,a2,a3,则求解方程x+y+z=P-a1-a2-a33:如果x,y,z均有上界的自然数,则使用容斥定理 ...

  10. 数据挖掘竞赛kaggle初战——泰坦尼克号生还预测

    1.题目 这道题目的地址在https://www.kaggle.com/c/titanic,题目要求大致是给出一部分泰坦尼克号乘船人员的信息与最后生还情况,利用这些数据,使用机器学习的算法,来分析预测 ...