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

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

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

当前回答

试试:

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

其他回答

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

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;

我尝试了下面的捕捉命令,它有助于在 /etc/yum.repos.d 我的存储库中搜索内容。

grep -Ril -e 'texttoSearch' /etc/yum.repos.d

有一个名为The Silversearcher的新工具

sudo apt install silversearcher-ag

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

你可以简单地使用

ag "Search query"

它会为你完成任务!

我很感动如何简单的吸引力使它与“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

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

您可以使用捕捉工具重复搜索当前文件夹,如:

grep -r "class foo" .

注意: -r - 重复搜索分支机构。

grep "class foo" **/*.c

如果你有错误,你的论点太长,考虑缩短你的搜索,或者使用找到合成代替,如:

find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'

或者使用Ripgrep。

rg "class foo" .

它比任何其他工具,如 GNU/BSD grep, ucg, ag, sift, ack, pt 或类似,因为它是建立在Rust的 regex 引擎的顶部,使用终端自动化,SIMD和攻击性字体优化,使搜索非常快。


-i - 不敏感的搜索. -I - 忽略二进制文件. -w - 搜索完整的单词(相反的部分单词匹配)。 -n - 显示您的匹配线. -C/--背景(例如 -C5) - 增加背景,所以你看到周围的代码. --color=auto - 标记匹配文本. -H - 显示文件名,在文本中找到。