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

find . | xargs grep "texthere" *

当前回答

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

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

其他回答

ag是我现在最喜欢的实现方式github.com/ggreer/the_silver_searcher。它基本上与ack相同,但还有一些优化。

这是一个简短的基准。我在每次测试前清除缓存(cfhttps://askubuntu.com/questions/155768/how-do-i-clean-or-disable-the-memory-cache )

ryan@3G08$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time grep -r "hey ya" .

real    0m9.458s
user    0m0.368s
sys 0m3.788s
ryan@3G08:$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time ack-grep "hey ya" .

real    0m6.296s
user    0m0.716s
sys 0m1.056s
ryan@3G08$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time ag "hey ya" .

real    0m5.641s
user    0m0.356s
sys 0m3.444s
ryan@3G08$ time ag "hey ya" . #test without first clearing cache

real    0m0.154s
user    0m0.224s
sys 0m0.172s

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

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

Solaris和HP-UX同意。

在IBM AIX Server(操作系统版本:AIX 5.2)中,使用:

find ./ -type f -print -exec grep -n -i "stringYouWannaFind" {} \; 

这将打印出文件中的路径/文件名和相对行号,如:

./inc/xxxx_x.h

2865:/**描述:stringYouWanna查找*/

无论如何,这对我来说很有用:)

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

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

这应该是有效的:

grep -R "texthere" *