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

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

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

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

其他回答

避免混乱并安装 ack-grep. 它消除了许多许可和引用问题。

apt-get install ack-grep

然后转到您想要搜索的目录,并运行下面的命令。

cd /
ack-grep "find my keyword"

如果您在 Git 存储库中,您可以使用:

git grep something
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

使用 grep -ilR:

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

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

我的使用案例是找到我写的Python代码,我写的JSON线的某种方式,我知道JSONL将是函数名称的一部分,到_JSON将出现在身体中,但没有更多的东西。

尽管有 50 个答案,但在同一文件中找到多个字符串(无论是在同一行中还是不在同一行中)并没有得到答案。

格雷普中的 -q 是安静的. 没有什么是打印,只有返回值是设置的. 因此,在结尾的 - 打印. 每个 -exec 只运行,如果前一个成功。

find . -type f -name "*.py" \
  -exec grep -q -e 'to_json' {} \; \
  -exec grep -q -e 'def\s.*jsonl' {} \; \
  -print