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

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

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

当前回答

Silver Searcher 是一个可怕的工具,但 ripgrep 可能甚至更好。

它在Linux、Mac和Windows上运行,并在几个月前在Hacker News上编写(它有一个链接到Andrew Gallant的博客,其中有一个GitHub链接):

Ripgrep - 新命令线搜索工具

其他回答

我很感动如何简单的吸引力使它与“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 /path -type f -exec grep -l "string" {} \;

评论的解释

查找是一个命令,允许您在特定路径的子目录中找到文件和其他对象,如目录和链接,如果您不指定该文件名称应满足的面具,则列出所有目录对象。

-type f specifies that it should proceed only files, not directories etc.
-exec grep specifies that for every found file, it should run grep command, passing its filename as an argument to it, by replacing {} with the filename

试试这

find . -type f -name some_file_name.xml -exec grep -H PUT_YOUR_STRING_HERE {} \;

有一个Ack工具,它会做你正在寻找的东西:

ack -i search_string folder_path/*

您可能会忽略 -i 对案例敏感搜索。

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

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

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

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

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

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

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