我如何在其文件内容中找到包含一个特定的文本序列的所有文件?

下一个不起作用. 它似乎显示系统中的每个单一文件。

find / -type f -exec grep -H 'text-to-find-here' {} \;

当前回答

我很感动如何简单的吸引力使它与“rl”:

grep -rl 'pattern_to_find' /path/where/to/find

-r to recursively find a file / directory inside directories..
-l to list files matching the 'pattern'

使用“r”而不“l”以查看文件名跟随文本中的模式!

grep -r 'pattern_to_find' /path/where/to/find

它只是完美工作......

其他回答

如果您的捕捉不支持重复搜索,您可以将搜索与 xargs 相结合:

find / -type f | xargs grep 'text-to-find-here'

我觉得这比找到 -exec 的格式更容易记住。

这将产生相匹配线的文件名和内容,例如。

/home/rob/file:text-to-find-here

您可能想添加到捕捉的选项旗帜:

-i - 案例不敏感搜索 -l - 只输出相匹配的行输出(而不是相匹配的行输出)

包含特定文本的文件名列表

首先,我相信你使用了 -H 而不是 -l. 你也可以尝试在引用中添加文本,其次是 {} \。

find / -type f -exec grep -l "text-to-find-here" {} \; 

例子

假设您正在寻找包含特定文本“Apache 许可证”的文件在您的目录中,它将显示一些类似于下面的结果(结果将根据您的目录内容不同)。

bash-4.1$ find . -type f -exec grep -l "Apache License" {} \; 
./net/java/jvnet-parent/5/jvnet-parent-5.pom
./commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.pom
./io/swagger/swagger-project/1.5.10/swagger-project-1.5.10.pom
./io/netty/netty-transport/4.1.7.Final/netty-transport-4.1.7.Final.pom
./commons-codec/commons-codec/1.9/commons-codec-1.9.pom
./commons-io/commons-io/2.4/commons-io-2.4.pom
bash-4.1$ 

取消案例敏感性

即使你不使用案例如“文本”与“文本”,你可以使用 -i 交换来忽略案例。

我尝试了下面的捕捉命令,它有助于在 /etc/yum.repos.d 我的存储库中搜索内容。

grep -Ril -e 'texttoSearch' /etc/yum.repos.d
grep "text-to-find-here" file_name

grep "text-to-find-here" directory_path/*

如果你想搜索当前的目录:

grep "text-to-find-here" *

试试这:

find . | xargs grep 'word' -sl