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

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

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

当前回答

我认为值得提到你如何找到:

所有包含至少一个文本的文件,其中包括大量的文本:

grep -rlf ../patternsFile.txt .

出口:

./file1  
./file2
./file4

上述,由每个文本组成:

cat ../patternsFile.txt | xargs -I{} sh -c "echo {}; grep -rl \"{}\" ."

出口:

pattern1
./file1  
./file2
pattern2
./file1  
./file4
pattern3
./file1  
./file2
./file4

请注意,为了不匹配模式File.txt本身,您需要添加一个目录(如上面的示例所示)。

其他回答

这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在Linux -

grep -inRsH “文本要被搜索” /path/to/dir(它可以是“。

i stands for ignore case distinctions R stands for recursive and it also includes symlinks. It is better to use 'R' instead of 'r' n stands for "it will print line number." s stands for "suppress error messages" H stands for "it will print the file name for each match"

您可以使用以下命令从文件中找到特定文本:

cat file | grep 'abc' | cut -d':' -f2

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

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

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

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

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

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

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

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

例子:

pt -e 'text to search'

正如彼得在以前的回答中提到的那样,所有以前的回答都建议抓住并找到。

但有一个更复杂的方式使用Gnome Commander与完美的GUI和数以百万计的选项自2001年以来,并找到文件只是其中之一。