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

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

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

当前回答

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

grep -rnw `pwd` -e "pattern"

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

这样:

格雷普 -rnw -e “模式”

格雷普 -rnw “模式”

他们会像上面那样做!

其他回答

你的命令是正确的. 你只需要添加 -l 来抓住:

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

您可以使用 ack. 它就像输入源代码. 您可以扫描您的整个文件系统。

只是做:

ack 'text-to-find-here'

在你的根目录中。

您也可以使用常规表达式,指定文件类型等。


更新

我刚刚发现The Silver Searcher,这类似于Ack,但比它快3至5倍,甚至忽略了来自.gitignore 文件的模式。

grep "text-to-find-here" file_name

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

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

grep "text-to-find-here" *

试试这:

find . | xargs grep 'word' -sl

找到与 xargs 是优先的,当有许多潜在的比赛可以通过. 它运行比其他选项更慢,但它总是工作. 正如一些发现,xargs 不处理文件与嵌入空间默认。

这里是 @RobEarl 的答案,增强,以便处理文件与空间:

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

下面是 @venkat 的答案,同样增强:

find . -name "*.txt" | xargs -d '\n' grep -i "text_pattern"

这里是 @Gert van Biljon的答案,同样增强:

find . -type f -name "*.*" -print0 | xargs -d '\n' --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"

以下是 @LetalProgrammer 的答案,同样增强:

alias ffind find / -type f | xargs -d '\n' grep

这里是 @Tayab Hussain的答案,同样增强:

find . | xargs -d '\n' grep 'word' -sl