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

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

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

当前回答

如果您严格想要使用查找,则使用查找 + 查找:

找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;

步骤:

使用查找搜索文件,执行捕获所有。

这给了你找到文件的权力。

使用 -name 模式,如果您只想捕捉某些文件:

找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;

您可以使用不同的搜索选项来改善您的文件搜索。

其他回答

下面的命令将为此做得很好:

find ./ -name "file_pattern_name"  -exec grep -r "pattern" {} \;

此外,请参见 The Platinium Searcher,这类似于 The Silver Searcher,并写在 Go。

例子:

pt -e 'text to search'

使用 grep -ilR:

grep -Ril "text-to-find-here" /

i 代表忽略案例(可选在您的案例中)。R 代表重复。l 代表“显示文件名,而不是结果本身”。 / 代表在您的机器的根源开始。

试试这:

find / -type f -name "*" -exec grep -il "String_to_search" {} \;

for i in /*;do grep -Ril "String_to_search" $i;done 2> /dev/null

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

首先,我相信你使用了 -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 交换来忽略案例。