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

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

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

当前回答

您可以使用下面的命令,因为您不想要文件名,但您想要从所有文件中搜索。

grep -e TEXT *.log | cut -d' ' --complement -s -f1

捕捉与 -e 选项是相当快的与其他选项相比,因为它是PATTERN 比赛

其他回答

要搜索字符串和输出,就是与搜索字符串相同的字符串:

for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done

吉:

for i in $(find /usr/share/applications -type f); \
do grep -i "web browser" "$i"; done

要显示包含搜索字符串的文件名:

for i in $(find /path/of/target/directory -type f); do if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done;

吉:

for i in $(find /usr/share/applications -type f); \
do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \
fi; done;

您可以使用下面的命令,因为您不想要文件名,但您想要从所有文件中搜索。

grep -e TEXT *.log | cut -d' ' --complement -s -f1

捕捉与 -e 选项是相当快的与其他选项相比,因为它是PATTERN 比赛

正如彼得在以前的回答中提到的那样,所有以前的回答都建议抓住并找到。

但有一个更复杂的方式使用Gnome Commander与完美的GUI和数以百万计的选项自2001年以来,并找到文件只是其中之一。

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

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

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 的文件名, 允许通过文件名。

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

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

步骤:

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

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

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

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

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