zoukankan      html  css  js  c++  java
  • [Linux]Linux环境,如何查找一个目录下所有包含特定字符串的文件

    在 Linux 环境中,如果想要查找一个目录下,所有包含某一个特定字符串的文件,可以使用 find 命令、xargs 命令 和 grep 命令的结合来完成查找。

    比如,目录结构如下:

    $ tree /refresh/home/aaa
    
    /refresh/home/aaa
    ├── aaa01.txt
    ├── aaa02.txt
    ├── bb
    │   ├── bb01.txt
    │   └── bb02.txt
    └── cc
        ├── cc01.txt
        └── cc02.txt
    
    2 directories, 6 files
    $
    

    各个文件的内容如下:

    $ cat /refresh/home/aaa/aaa01.txt
    This is my word aaa001
    $
    
    $ cat /refresh/home/aaa/aaa02.txt
    This is my word aaa02
    $
    
    $ cat /refresh/home/aaa/bb/bb01.txt
    Thi is my word bb01
    
    $ cat /refresh/home/aaa/bb/bb02.txt
    This is my word bb02
    
    $ cat /refresh/home/aaa/cc/cc01.txt
    Thi is my word cc01
    
    $ cat /refresh/home/aaa/cc/cc02.txt
    This is my word cc02
    

    现在,我要查找,在 /refresh/home/aaa 目录及其子目录中,所有 包含 my 这个字符串的所有文件,我可以这样执行:

    $ find /refresh/home/aaa | xargs grep "my"
    /refresh/home/aaa/bb/bb02.txt:This is my word bb02
    /refresh/home/aaa/bb/bb01.txt:Thi is my word bb01
    /refresh/home/aaa/aaa01.txt:This is my word aaa001
    /refresh/home/aaa/cc/cc02.txt:This is my word cc02
    /refresh/home/aaa/cc/cc01.txt:Thi is my word cc01
    /refresh/home/aaa/aaa02.txt:This is my word aaa02
    $
    
  • 相关阅读:
    流程控制
    小结
    运算符
    进制之间的转换
    自动类型转换和强制类型转换
    变量
    关键字,标识符,
    NGINX 做TCP转发(端口转发)并记录日志
    redash安装
    解决 es CircuitBreakingException 问题(Data too large Error)
  • 原文地址:https://www.cnblogs.com/gaojian/p/14631637.html
Copyright © 2011-2022 走看看