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

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

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

当前回答

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

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

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

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

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

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

其他回答

我很感动如何简单的吸引力使它与“rl”:

grep -rl 'pattern_to_find' /path/where/to/find

-r to recursively find a file / directory inside directories..
-l to list files matching the 'pattern'

使用“r”而不“l”以查看文件名跟随文本中的模式!

grep -r 'pattern_to_find' /path/where/to/find

它只是完美工作......

试试这:

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

for i in /*;do grep -Ril "String_to_search" $i;done 2> /dev/null
grep "text-to-find-here" file_name

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

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

grep "text-to-find-here" *

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

只是做:

ack 'text-to-find-here'

在你的根目录中。

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


更新

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

重复和案例不敏感的捕捉与线号:

grep -inr "Text" folder/to/be/searched/