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

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

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

当前回答

您可以使用捕捉工具重复搜索当前文件夹,如:

grep -r "class foo" .

注意: -r - 重复搜索分支机构。

grep "class foo" **/*.c

如果你有错误,你的论点太长,考虑缩短你的搜索,或者使用找到合成代替,如:

find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'

或者使用Ripgrep。

rg "class foo" .

它比任何其他工具,如 GNU/BSD grep, ucg, ag, sift, ack, pt 或类似,因为它是建立在Rust的 regex 引擎的顶部,使用终端自动化,SIMD和攻击性字体优化,使搜索非常快。


-i - 不敏感的搜索. -I - 忽略二进制文件. -w - 搜索完整的单词(相反的部分单词匹配)。 -n - 显示您的匹配线. -C/--背景(例如 -C5) - 增加背景,所以你看到周围的代码. --color=auto - 标记匹配文本. -H - 显示文件名,在文本中找到。

其他回答

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

只是做:

ack 'text-to-find-here'

在你的根目录中。

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


更新

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

试试这:

find / -type f -name "*" -exec grep -il "String_to_search" {} \;

for i in /*;do grep -Ril "String_to_search" $i;done 2> /dev/null

做下列事:

grep -rnw '/path/to/somewhere/' -e 'pattern'

-r 或 -R 是重复的, -n 是字符号, -w 是相匹配整个单词的。 -l (较低的 L 案例) 可以添加到只提供相匹配文件的文件名。

与此同时, - 排除, - 包括, - 排除-dir 旗帜可以用于有效的搜索:

这只会通过具有.c 或.h 扩展的文件进行搜索: grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern" 这将排除与.o 扩展结束的所有文件进行搜索: grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern" 对于目录,可以使用 --exclude-dir 参数排除一个或多个目录。

这对我来说非常好,为了实现几乎与你的同样的目的。

更多选项,看看男人抓住。

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

alias ffind find / -type f | xargs grep

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

ffind 'text-to-find-here'

下面是可以用于搜索文件的几个命令列表。

grep "text string to search” directory-path

grep [option] "text string to search” directory-path

grep -r "text string to search” directory-path

grep -r -H "text string to search” directory-path

egrep -R "word-1|word-2” directory-path

egrep -w -R "word-1|word-2” directory-path