• Linux使用find命令查找文件


    简介

    https://zhuanlan.zhihu.com/p/550924132

      Linux系统中的find命令是一种功能丰富的工具,可以帮助我们快速查找文件或目录。本教程将向您展示如何使用Linux find命令来查找您需要的文件。
      在使用Linux find命令之前,您需要了解其基本语法。Linux find命令的基本语法如下:

    find [path] [expression]
    
    • 1

      其中,path表示要查找的目录路径,expression表示查找表达式,用于指定要查找的文件类型、名称、大小等条件。

      find命令的作用是在目录层次结构中搜索文件所在的位置,此命令可以使用的参数很多,同时支持正则表达式,结合管道符后能够实现更加复杂的功能,是必须掌握的命令之一。
      通常find是从根目录开始全盘搜索,不同于其他几个搜索文件的命令,find搜索时会消耗较多的系统资源,在服务器负载较高的时候,不建议从根目录开始搜索。

    语法格式

    语法格式:find 【路径】【参数】

    SYNOPSIS
    find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path…] [expression]

    基本参数

    此命令的常用参数有以下这些,以表格形式显示:

    -name匹配文件的名称
    -user匹配用户的文件(所有者)
    -group匹配组的文件(所有组)
    -mtime -n +n匹配修改内容的时间,-n表示n天之内,+n表示n天之前
    -atime -n +n匹配访问文件的时间,-n表示n天之内,+n表示n天之前
    -ctime -n +n匹配改动文件的时间,-n表示n天之内,+n表示n天之前
    -perm匹配文件权限
    -size匹配文件的大小,单位k M,+nk表示查找大于n的文件,-nk表示查找小于n的文件
    -exec { } ;后面可跟用于进一步处理搜索结果的命令
    -prune忽略某个目录
    -nouser匹配不是这个用户的文件
    -nogroup匹配不是这个组的文件
    -type匹配文件类型(b d c p f l)

    其中,-type参数的文件类型:

    b:块设备文件
    d:目录文件
    c:字符设备文件
    p:管道文件
    f :文本文件
    l :链接文件

    参考实例

    根目录下文件名称的例子

    find / -name "000"
    
    • 1

    上述命令将在服务器的根目录中查找包含"000"的文件或目录。

    在root/data目录下搜索*.txt的文件名

    [root@localhost ~]# find /root/data -name "*.txt"
    /root/data/1.txt
    /root/data/2.txt
    /root/data/3.txt
    /root/data/4.txt
    /root/data/5.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在/var/log目录搜索指定后缀的文件 , -iname表示不区分大小写的的文件名称

    [root@localhost ~]# find /var/log -type f -iname "*.log"
    /var/log/tuned/tuned.log
    /var/log/audit/audit.log
    /var/log/anaconda/anaconda.log
    /var/log/anaconda/X.log
    /var/log/anaconda/program.log
    /var/log/anaconda/packaging.log
    /var/log/anaconda/storage.log
    ......
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在/var/log目录搜索指定后缀不是.log的文件

    [root@localhost ~]# find /var/log -type f ! -name ".log" | wc -l
    71
    
    • 1
    • 2

    指定路径下特定类型的例子

    除了查找特定关键词的文件或目录外,您还可以使用Linux find命令的其他选项来进一步细化您的搜索。
    要查找特定类型的文件,您可以使用-type选项。

    find /path/to/search -type f -name "*.txt"
    
    • 1

      在上述命令中,/path/to/search是您要查找的目录路径,-type f表示要查找的是文件类型,-name "*.txt"表示要查找以.txt结尾的文件。

    指定路径、文件类型特定文件名称的例子

    find /path/to/search -type f -name "file.txt"
    
    • 1

    指定路径、文件类型特定文件大小的例子

    要查找特定大小的文件,可以使用-size选项。例如,要查找大小为10MB的文件,可以使用以下命令:

    find /path/to/search -type f -size 10M
    
    • 1

    在/etc目录下搜索大于5M,小于10M的文件

    [root@localhost ~]# find /etc -type f -size +5M -and -size -10M 
    
    
    • 1
    • 2

    指定路径、文件类型 查找近期修改时间的例子

    如果您想查找近期修改的文件,可以使用-mtime选项。例如,要查找近期7天内修改过的文件,可以使用以下命令:

    find /path/to/search -type f -mtime -7
    
    • 1

    搜索一天以内最后修改时间的文件;并将文件删除
    使用-exec参数将前面的文件进行处理,也可使用find配合xargs将文件进行删除。

    [root@localhost ~]# find /root/data -mtime -1 
    /root/data
    /root/data/1.txt
    /root/data/2.txt
    /root/data/3.txt
    /root/data/4.txt
    /root/data/5.txt
    [root@localhost ~]# find /root/data -mtime -1 -exec rm -f {} \; 
     
    [root@localhost ~]# find /root/data -mtime -1 |xargs -i rm -f {}
     
    [root@localhost ~]# ll /root/data/
    总用量 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    指定路径、文件类型 查找空文件或目录的例子

    要查找空文件或目录,可以使用-empty选项。例如,要查找空目录,可以使用以下命令:

    find /path/to/search -type d -empty
    
    • 1

    指定路径、文件类型 查找特定权限文件的例子

    要查找特定权限的文件,可以使用-perm选项。例如,要查找所有小伙伴可读、可写和可执行的文件,可以使用以下命令:

    find /path/to/search -type f -perm 777
    
    • 1

    搜索指定用户的文件

    [root@localhost ~]# find / -type f -user host 
    
    • 1

    搜索指定组的目录

    [root@localhost ~]# find / -type d -group host
    /var/tmp/yum-host-u08wM2
    /var/tmp/yum-host-u08wM2/x86_64
    /var/tmp/yum-host-u08wM2/x86_64/7
    [root@localhost ~]# find / -type d -group host | wc -l
    18
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    组合多个选项

    您还可以将多个选项组合在一起来查找更精确的结果。例如,要查找所有的图片文件(.jpg或.png)和大小不超过1MB的文件,可以使用以下命令:

    find /path/to/search -type f \( -name "*.jpg" -o -name "*.png" \) -size -1M
    
    • 1

    通过组合不同的选项,您可以根据自己的需求来定制查找条件,从而更加准确地找到所需的文件。

  • 相关阅读:
    Ubuntu22.04 安装配置流水账
    数据驭王: PostgreSQL教程指南解密
    Spring IoC & DI 使⽤
    音视频项目—基于FFmpeg和SDL的音视频播放器解析(十六)
    苹果开发者账号持有人变更隐藏在哪里??
    BSP3 电力监控仪(功率监控仪)端子定义和接线
    力扣(LeetCode)算法_C++——移位字符串分组
    【JVM篇】有哪些垃圾回收算法
    速盾网络:cdn加速技术和云计算的区别
    [RCTF 2019]nextphp
  • 原文地址:https://blog.csdn.net/pfl_327/article/details/134000090