• Linux 文件搜索命令Find详解


    Linux 文件搜索命令Find详解

    1. 文件搜索命令Find

    注意:

    1. 服务器高峰时建议不要使用搜索命令或者搜索条件越多越好,尽量精准

    2. 如果包含路径与当前搜索文件名相同,可以使用转义字符或者引号:find . -name “init*”

    3. 使用-type d 搜索目录 f搜索文件

    1.1 基本搜索功能

    find [搜索范围] [匹配条件]

    1. 根据文件名搜索
    find . -name xxx
    
    • 1
    • 2
    1.2 模糊搜索
    1. *号使用
    find . -name xxx* 表示以xxx开头
    2. 使用?占位符
    find . -name xxx??? 以xxx开头后有三个字符的
    [root@localhost ~]# find . -name init??
    ./initit
    3. linux中区分大小写 使用-iname 可以不区分
    为了不区分可以使用:find . -iname xxx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1.4 固定查找大小

    注意要把大小换成数据块

    1. 指定大小可以使用 -n +n n 
    -n:小于
    +n:大于
    n:等于
    例如:查找大于100MB的文件
    # 这么写是错误的  find . -size +100 
    因为后后面的n大小是数据块,linux一个数据块大小是512B=0.5k
    所以100MB=102400KB=204800B
    find . -size +204800
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1.5 根据所有者查找
    find . -user shenchao
    如果是根据组查找使用-group即可
    
    • 1
    • 2
    1.6 根据时间属性来查询
    查找当前目录下5分钟内被修改的属性的文件和目录
    find . -cmin -5
    参数
    -amin 访问时间
    -cmin change时间 更改属性,比如权限和所属等
    -mmin 文件内容,文件内容修改时间
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1.7 逻辑查询
    find . -size +1000 -a -size -2000
    当前目录下查找大于500k和 小于1000k的文件
    -a:and 两个条件都满足
    -o:or 两个满足一个即可
    
    • 1
    • 2
    • 3
    • 4
    1.8 其他选项
    1. 根据查找结果查询其他内容

      -exec /-ok

      -exec:直接执行无需确认
      -ok:需要确认一下
      find . -name test -exec ls -ls {} \;
      其中{}表示find查询的结果,\;为固定格式
      =====================================
      [root@localhost ~]# find . -name test -exec ls -ls {} \;
      总用量 8
      0 drwxr-xr-x. 2 root root 191 816 12:37 bin
      0 drwxr-xr-x. 3 root root  23 816 12:36 lib
      0 lrwxrwxrwx. 1 root root   3 816 12:36 lib64 -> lib
      4 -rw-r--r--. 1 root root  61 816 12:37 pip-selfcheck.json
      4 -rw-r--r--. 1 root root  69 816 12:36 pyvenv.cfg
      [root@localhost ~]# find . -name test -ok ls -ls {} \;
      < ls ... ./test > ? y
      总用量 8
      0 drwxr-xr-x. 2 root root 191 816 12:37 bin
      0 drwxr-xr-x. 3 root root  23 816 12:36 lib
      0 lrwxrwxrwx. 1 root root   3 816 12:36 lib64 -> lib
      4 -rw-r--r--. 1 root root  61 816 12:37 pip-selfcheck.json
      4 -rw-r--r--. 1 root root  69 816 12:36 pyvenv.cfg
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
    2. 查询类型

      -type f 文件

      -type d 目录

      -type l 软链接文件

    3. 根据i节点查找,可以通过这个删除一些比较特殊的文件,比如带空格的文件,可以通过i节点删除

      -inum 查询i节点

      [root@localhost ~]# ls -i
       8409167 anaconda-ks.cfg  13565830 lib                          8409205 pybin        8409191 tet.py
      12911665 init              9342880 neo4j3528.zip                1429452 test         8409172 xxx.log
           341 initit           12911648 neo4j-community-3.5.28       8476141 Test.class
       8409190 INITTTT           8409171 neo4j-community-3.5.28.zip   8476142 Test.java
       
      [root@localhost ~]# find . -inum 341 -ok rm -r {} \;
      < rm ... ./initit > ? y
      find: ‘./initit’: 没有那个文件或目录
      [root@localhost ~]# ls -i
       8409167 anaconda-ks.cfg   9342880 neo4j3528.zip                8476141 Test.class
      12911665 init             12911648 neo4j-community-3.5.28       8476142 Test.java
       8409190 INITTTT           8409171 neo4j-community-3.5.28.zip   8409191 tet.py
      13565830 lib               8409205 pybin                        8409172 xxx.log
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

    在这里插入图片描述

  • 相关阅读:
    【K8S系列】深入解析k8s 网络插件—Antrea
    前端开发之el-table 表头不换行且宽度自适应
    论文阅读【Semantic Tag Augmented XlanV Model for Video Captioning】
    PySpark 概述
    GitHub&Gitee&Gitlab&极狐(JihuLab)同时生成并配置SSH公私钥详细过程
    软考系统架构设计师考试冲刺攻略
    常用的主流音乐编曲软件有哪些?
    Mybatis中拦截器的使用场景和技巧
    React函数式组件渲染、useEffect顺序总结
    monaco-editor 行号、内容装饰器
  • 原文地址:https://blog.csdn.net/weixin_44331765/article/details/126692090