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

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

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

当前回答

希望这就是帮助......

要在输出中提供更多信息,例如,在文件中获取字符号,文本可以如下:

find . -type f -name "*.*" -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searthtext"

如果您有一个想法,文件类型是什么,您可以通过指定文件类型扩展来缩小您的搜索,在此情况下,.pas 或.dfm 文件:

find . -type f \( -name "*.pas" -o -name "*.dfm" \) -print0 | xargs --null grep --with-filename --line-number --no-messages --color --ignore-case "searchtext"

以下是选项的简短解释:

在搜索中,从当前目录中指定 - 名称“**” : 所有文件( - 名称“*.pas” -o - 名称“*.dfm” ) : 只有 *.pas 或 *.dfm 文件, 或 与 -o -type f 指定, 您正在寻找文件 -print0 和 -null 是关键的, 将文件名从搜索中转移到嵌入在 xargs 的文件名, 允许通过文件名。

其他回答

试试:

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

您也可以使用 awk:

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

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

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

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

有一个名为The Silversearcher的新工具

sudo apt install silversearcher-ag

它与 Git 和其他 VCS 密切合作,所以您不会在.git 或其他目录中获得任何东西。

你可以简单地使用

ag "Search query"

它会为你完成任务!

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

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