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

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

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

当前回答


如果使用在您的例子中找到相似的,最好添加 -s ( - 没有消息) 捕捉,并在命令结束时 2>/dev/null 避免大量的许可拒绝的消息由捕捉发送,并找到:

find / -type f -exec grep -sH 'text-to-find-here' {} \; 2>/dev/null

更快、更容易的工具在同一用途中存在 - 请参见下面. 最好尝试它们,只要它们在您的平台上可用,当然:

更快、更容易的替代品

RipGrep - 最快的搜索工具周围:

rg 'text-to-find-here' / -l

ag 'text-to-find-here' / -l

ACK:

ack 'text-to-find-here' / -l


警告:除非你真的不能避免,不要从“/”(根目录)搜索,以避免漫长而不有效的搜索!因此,在上面的例子中,你最好用一个子目录名称替换“/”,例如“/home”取决于你实际上想要搜索的地方。

其他回答

如果您严格想要使用查找,则使用查找 + 查找:

找到 /path/to/somewhere/ -type f -exec grep -nw 'textPattern' {} \;

步骤:

使用查找搜索文件,执行捕获所有。

这给了你找到文件的权力。

使用 -name 模式,如果您只想捕捉某些文件:

找到 /path/to/somewhere/ -type f -name \*.cpp -exec grep -nw 'textPattern' {} \;

您可以使用不同的搜索选项来改善您的文件搜索。

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

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

试试:

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' {} \;

GUI Search Alternative - 用于桌面使用:

因为问题不准确要求命令。

Searchmonkey: Advanced file search tool without having to index your system using regular expressions. 图形相当于查找/查找. 可用于 Linux (Gnome/KDE/Java) 和 Windows (Java) - 开源 GPL v3

特性:

Advanced Regular Expressions Results shown in-context Search containing text Panel to display line containing text 新 2018 更新等。

下载 - 链接:

主页: http://searchmonkey.embeddediq.com/ 下载: http://searchmonkey.embeddediq.com/index.php/download-latest Repo: https://sourceforge.net/projects/searchmonkey/files/

屏幕射击:

此分類上一篇

可以使用,即使我们不寻找一个链条。

只是跑步,

grep -RIl "" .

将打印到所有文本文件的路径,即只有可打印的字符的文件。