我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
我认为值得提到你如何找到:
所有包含至少一个文本的文件,其中包括大量的文本:
grep -rlf ../patternsFile.txt .
出口:
./file1
./file2
./file4
上述,由每个文本组成:
cat ../patternsFile.txt | xargs -I{} sh -c "echo {}; grep -rl \"{}\" ."
出口:
pattern1
./file1
./file2
pattern2
./file1
./file4
pattern3
./file1
./file2
./file4
请注意,为了不匹配模式File.txt本身,您需要添加一个目录(如上面的示例所示)。
其他回答
试试:
find . -name "*.txt" | xargs grep -i "text_pattern"
如果您在 Git 存储库中,您可以使用:
git grep something
安德鲁·加兰特的博客声称:“Ripgrep 比 {grep, ag, git grep, ucg, pt, sift} 更快”(一些人分享的声明,这就是为什么一个功能比较是有用的)。 特别有兴趣的是他关于 regex 实施和漏洞的部分. 下一个命令搜索所有文件,包括隐藏和可执行: $ rg -uuu foobar 银搜索器(ag)声称它是 5-10 倍的速度。
虽然可以扫描外部程序,执行搜索,阻止其输出并处理它,呼叫API是电源和性能的途径。
你的命令是正确的. 你只需要添加 -l 来抓住:
find / -type f -exec grep -l 'text-to-find-here' {} \;
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