我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
我如何在其文件内容中找到包含一个特定的文本序列的所有文件?
下一个不起作用. 它似乎显示系统中的每个单一文件。
find / -type f -exec grep -H 'text-to-find-here' {} \;
当前回答
grep -insr "pattern" *
i: 在 PATTERN 和输入文件中忽略案例区别. n: 在输入文件中预定每个输出线的 1 行号. s: 删除关于不存在或不可读的文件的错误消息. r: 阅读每个目录下的所有文件,重复。
其他回答
您可以使用 ripgrep 将遵守默认项目的.gitignore 文件。
此分類上一篇
拒绝许可的错误:
rg -i rustacean 2> /dev/null
将默认错误(stderr)输出转向 /dev/null。
重复和案例不敏感的捕捉与线号:
grep -inr "Text" folder/to/be/searched/
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
您可以使用 ack. 它就像输入源代码. 您可以扫描您的整个文件系统。
只是做:
ack 'text-to-find-here'
在你的根目录中。
您也可以使用常规表达式,指定文件类型等。
更新
我刚刚发现The Silver Searcher,这类似于Ack,但比它快3至5倍,甚至忽略了来自.gitignore 文件的模式。
A Simple find can work handy. alias it in your ~/.bashrc 文件:
alias ffind find / -type f | xargs grep
开始一个新的终端和问题:
ffind 'text-to-find-here'