如何递归地grep所有目录和子目录?

find . | xargs grep "texthere" *

当前回答

只是为了好玩,如果@christangrant的答案太多而无法输入,可以快速搜索*.txt文件:-)

grep-r文本此处|grep.txt文件

其他回答

在POSIX系统中,找不到grep的-r参数和grep-rn“东西”。不会运行,但如果使用find命令,它将:

查找-键入f-exec grep-n“stuff”{}\-打印

Solaris和HP-UX同意。

有关可用标志的列表:

grep --help 

返回当前目录中此处的regexp文本的所有匹配项,以及相应的行号:

grep -rn "texthere" .

返回文本的所有匹配项,从根目录开始,具有相应的行号,忽略大小写:

grep -rni "texthere" /

此处使用的标志:

-r递归-n带输出的打印行号-我不理这个案子

2018年,您希望使用ripgrep或白银搜索器,因为它们比其他搜索器快得多。

下面是一个包含336个一级子目录的目录:

% find . -maxdepth 1 -type d | wc -l
     336

% time rg -w aggs -g '*.py'
...
rg -w aggs -g '*.py'  1.24s user 2.23s system 283% cpu 1.222 total

% time ag -w aggs -G '.*py$'
...
ag -w aggs -G '.*py$'  2.71s user 1.55s system 116% cpu 3.651 total

% time find ./ -type f -name '*.py' | xargs grep -w aggs
...
find ./ -type f -name '*.py'  1.34s user 5.68s system 32% cpu 21.329 total
xargs grep -w aggs  6.65s user 0.49s system 32% cpu 22.164 total

在OSX上,这将安装rigrep:brew install rigrep。这将安装silver搜索器:brew install the _silver_searcher。

这应该是有效的:

grep -R "texthere" *

只是为了好玩,如果@christangrant的答案太多而无法输入,可以快速搜索*.txt文件:-)

grep-r文本此处|grep.txt文件