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

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

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

当前回答

试试这:

find . | xargs grep 'word' -sl

其他回答

grep "text-to-find-here" file_name

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

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

grep "text-to-find-here" *

有一个名为The Silversearcher的新工具

sudo apt install silversearcher-ag

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

你可以简单地使用

ag "Search query"

它会为你完成任务!

您可以使用以下命令从文件中找到特定文本:

cat file | grep 'abc' | cut -d':' -f2

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

首先,我相信你使用了 -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 交换来忽略案例。

A Simple find can work handy. alias it in your ~/.bashrc 文件:

alias ffind find / -type f | xargs grep

开始一个新的终端和问题:

ffind 'text-to-find-here'