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

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

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

当前回答

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

只是做:

ack 'text-to-find-here'

在你的根目录中。

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


更新

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

其他回答

使用 grep -ilR:

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

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

您可以使用以下命令从文件中找到特定文本:

cat file | grep 'abc' | cut -d':' -f2

我尝试了下面的捕捉命令,它有助于在 /etc/yum.repos.d 我的存储库中搜索内容。

grep -Ril -e 'texttoSearch' /etc/yum.repos.d

找到与 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

试试:

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

将搜索所有文件系统,因为 / 是根文件夹。

Home 文件夹使用:

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

目前使用文件夹:

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