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

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

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

当前回答

有一个名为The Silversearcher的新工具

sudo apt install silversearcher-ag

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

你可以简单地使用

ag "Search query"

它会为你完成任务!

其他回答

grep -insr "pattern" *

i: 在 PATTERN 和输入文件中忽略案例区别. n: 在输入文件中预定每个输出线的 1 行号. s: 删除关于不存在或不可读的文件的错误消息. r: 阅读每个目录下的所有文件,重复。

按要求轻松自定义下面的命令,并从文件中重复找到任何行。

grep -i hack $(find /etc/ -type f)

有一个Ack工具,它会做你正在寻找的东西:

ack -i search_string folder_path/*

您可能会忽略 -i 对案例敏感搜索。

grep "text-to-find-here" file_name

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

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

grep "text-to-find-here" *
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