[Linux]Linux环境,如何查找一个目录下所有包含特定字符串的文件
在 Linux 环境中,如果想要查找一个目录下,所有包含某一个特定字符串的文件,可以使用 find 命令、xargs 命令 和 grep 命令的结合来完成查找。
比如,目录结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ 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 $ |
各个文件的内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $ 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 这个字符串的所有文件,我可以这样执行:
1 2 3 4 5 6 7 8 | $ 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 $ |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
2014-04-08 连接到win2003的远程桌面,客户端要如何操作