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

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

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

当前回答

grep "text-to-find-here" file_name

grep "text-to-find-here" directory_path/*

如果你想搜索当前的目录:

grep "text-to-find-here" *

其他回答

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

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

包含特定文本的文件名列表

首先,我相信你使用了 -H 而不是 -l. 你也可以尝试在引用中添加文本,其次是 {} \。

find / -type f -exec grep -l "text-to-find-here" {} \; 

例子

假设您正在寻找包含特定文本“Apache 许可证”的文件在您的目录中,它将显示一些类似于下面的结果(结果将根据您的目录内容不同)。

bash-4.1$ find . -type f -exec grep -l "Apache License" {} \; 
./net/java/jvnet-parent/5/jvnet-parent-5.pom
./commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.pom
./io/swagger/swagger-project/1.5.10/swagger-project-1.5.10.pom
./io/netty/netty-transport/4.1.7.Final/netty-transport-4.1.7.Final.pom
./commons-codec/commons-codec/1.9/commons-codec-1.9.pom
./commons-io/commons-io/2.4/commons-io-2.4.pom
bash-4.1$ 

取消案例敏感性

即使你不使用案例如“文本”与“文本”,你可以使用 -i 交换来忽略案例。

使用 pwd 从您所在的任何目录中搜索,向下滑动

grep -rnw `pwd` -e "pattern"

取决于您正在使用的 grep 版本,您可以忽略 pwd. 在更新的版本中,如果没有目录,则似乎是 grep 的默认情况。

这样:

格雷普 -rnw -e “模式”

格雷普 -rnw “模式”

他们会像上面那样做!

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

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

Ripgrep - 新命令线搜索工具

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

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