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

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

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

当前回答

试试这

find . -type f -name some_file_name.xml -exec grep -H PUT_YOUR_STRING_HERE {} \;

其他回答

您可以使用下面的命令,因为您不想要文件名,但您想要从所有文件中搜索。

grep -e TEXT *.log | cut -d' ' --complement -s -f1

捕捉与 -e 选项是相当快的与其他选项相比,因为它是PATTERN 比赛

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

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

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本身,您需要添加一个目录(如上面的示例所示)。

您也可以使用 awk:

awk '/^(pattern)/{print}' /path/to/find/*

模式是您想要在文件中匹配的行。

有一个名为The Silversearcher的新工具

sudo apt install silversearcher-ag

它与 Git 和其他 VCS 密切合作,所以您不会在.git 或其他目录中获得任何东西。

你可以简单地使用

ag "Search query"

它会为你完成任务!

find /path -type f -exec grep -l "string" {} \;

评论的解释

查找是一个命令,允许您在特定路径的子目录中找到文件和其他对象,如目录和链接,如果您不指定该文件名称应满足的面具,则列出所有目录对象。

-type f specifies that it should proceed only files, not directories etc.
-exec grep specifies that for every found file, it should run grep command, passing its filename as an argument to it, by replacing {} with the filename