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

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

find / -type f -exec grep -H '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

其他回答

你可以使用:

grep -r "string to be searched"  /path/to/dir

r 是可重复的,因此将在指定的路径和其子指南中进行搜索,这将告诉您文件名以及在文件中印刷线的字符串。

或类似于您正在尝试的命令(例如: )在所有JavaScript文件中搜索( (*.js):

find . -name '*.js' -exec grep -i 'string to search for' {} \; -print

这将打印文本显示的文件中的行,但它不会打印文件名。

除了此命令之外,我们还可以写下: grep -rn “String to search” /path/to/directory/or/file -r: recursive search n: line number will be shown for matches

试试:

find . -name "*.txt" | xargs grep -i "text_pattern"

使用 pwd 从您所在的任何目录中搜索,向下滑动

grep -rnw `pwd` -e "pattern"

取决于您正在使用的 grep 版本,您可以忽略 pwd. 在更新的版本中,如果没有目录,则似乎是 grep 的默认情况。

这样:

格雷普 -rnw -e “模式”

格雷普 -rnw “模式”

他们会像上面那样做!

使用:

grep -Erni + "text you wanna search"

命令将重复搜索当前目录的所有文件和目录,并打印结果。

注意:如果您的格雷普输出没有颜色,您可以通过在您的壳源文件中使用格雷普=‘格雷普=颜色=总是’的标志来更改它。

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

例子:

pt -e 'text to search'