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

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

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

当前回答

您可以使用 ripgrep 将遵守默认项目的.gitignore 文件。

此分類上一篇

拒绝许可的错误:

rg -i rustacean 2> /dev/null

将默认错误(stderr)输出转向 /dev/null。

其他回答

我认为值得提到你如何找到:

所有包含至少一个文本的文件,其中包括大量的文本:

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本身,您需要添加一个目录(如上面的示例所示)。

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

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

重复和案例不敏感的捕捉与线号:

grep -inr "Text" folder/to/be/searched/

您可以使用 ack. 它就像输入源代码. 您可以扫描您的整个文件系统。

只是做:

ack 'text-to-find-here'

在你的根目录中。

您也可以使用常规表达式,指定文件类型等。


更新

我刚刚发现The Silver Searcher,这类似于Ack,但比它快3至5倍,甚至忽略了来自.gitignore 文件的模式。

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