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

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

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

当前回答

您也可以使用 awk:

awk '/^(pattern)/{print}' /path/to/find/*

模式是您想要在文件中匹配的行。

其他回答

grep -lrnw '/root/Desktop/ipozal' -e 'geolocation'

例如:

我的文件夹名称是“Ipozal”它放在“/root/Desktop”我想在所有文件中找到此文本“地理位置”

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

只是做:

ack 'text-to-find-here'

在你的根目录中。

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


更新

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

下面是可以用于搜索文件的几个命令列表。

grep "text string to search” directory-path

grep [option] "text string to search” directory-path

grep -r "text string to search” directory-path

grep -r -H "text string to search” directory-path

egrep -R "word-1|word-2” directory-path

egrep -w -R "word-1|word-2” directory-path

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

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

试试这:

find / -type f -name "*" -exec grep -il "String_to_search" {} \;

for i in /*;do grep -Ril "String_to_search" $i;done 2> /dev/null