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

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

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

当前回答

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

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

其他回答

如果您的捕捉不支持重复搜索,您可以将搜索与 xargs 相结合:

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

我觉得这比找到 -exec 的格式更容易记住。

这将产生相匹配线的文件名和内容,例如。

/home/rob/file:text-to-find-here

您可能想添加到捕捉的选项旗帜:

-i - 案例不敏感搜索 -l - 只输出相匹配的行输出(而不是相匹配的行输出)

您也可以使用 awk:

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

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

使用 grep -ilR:

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

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

如果你有一组文件,你会一直在检查,你可以列出他们的路径,例如:

alias fd='find . -type f -regex ".*\.\(inc\|info\|module\|php\|test\|install\|uninstall\)"'

然后你可以简单地过滤列表如下:

grep -U -l $'\015' $(fd)

将列表 fd 过滤到包含 CR 模式的文件。

我发现,将我感兴趣的文件会帮助我创建更容易的脚本,然后总是试图记住如何获得所有这些文件。 回复的物品工作,但早或晚你将不得不争取分散特定的文件类型。

试试这:

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

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