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

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

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

当前回答

grep -lrnw '/root/Desktop/ipozal' -e 'geolocation'

例如:

我的文件夹名称是“Ipozal”它放在“/root/Desktop”我想在所有文件中找到此文本“地理位置”

其他回答

安德鲁·加兰特的博客声称:“Ripgrep 比 {grep, ag, git grep, ucg, pt, sift} 更快”(一些人分享的声明,这就是为什么一个功能比较是有用的)。 特别有兴趣的是他关于 regex 实施和漏洞的部分. 下一个命令搜索所有文件,包括隐藏和可执行: $ rg -uuu foobar 银搜索器(ag)声称它是 5-10 倍的速度。

虽然可以扫描外部程序,执行搜索,阻止其输出并处理它,呼叫API是电源和性能的途径。

此外,请参见 The Platinium Searcher,这类似于 The Silver Searcher,并写在 Go。

例子:

pt -e 'text to search'
find /path -type f -exec grep -l "string" {} \;

评论的解释

查找是一个命令,允许您在特定路径的子目录中找到文件和其他对象,如目录和链接,如果您不指定该文件名称应满足的面具,则列出所有目录对象。

-type f specifies that it should proceed only files, not directories etc.
-exec grep specifies that for every found file, it should run grep command, passing its filename as an argument to it, by replacing {} with the filename

使用 grep -ilR:

grep -Ril "text-to-find-here" /

i 代表忽略案例(可选在您的案例中)。R 代表重复。l 代表“显示文件名,而不是结果本身”。 / 代表在您的机器的根源开始。

避免混乱并安装 ack-grep. 它消除了许多许可和引用问题。

apt-get install ack-grep

然后转到您想要搜索的目录,并运行下面的命令。

cd /
ack-grep "find my keyword"