如何递归地grep所有目录和子目录?
find . | xargs grep "texthere" *
如何递归地grep所有目录和子目录?
find . | xargs grep "texthere" *
当前回答
只有文件名也很有用
grep -r -l "foo" .
其他回答
有关可用标志的列表:
grep --help
返回当前目录中此处的regexp文本的所有匹配项,以及相应的行号:
grep -rn "texthere" .
返回文本的所有匹配项,从根目录开始,具有相应的行号,忽略大小写:
grep -rni "texthere" /
此处使用的标志:
-r递归-n带输出的打印行号-我不理这个案子
在POSIX系统中,找不到grep的-r参数和grep-rn“东西”。不会运行,但如果使用find命令,它将:
查找-键入f-exec grep-n“stuff”{}\-打印
Solaris和HP-UX同意。
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
要查找路径递归包含特定字符串的文件名,请使用以下命令对于UNIX:
find . | xargs grep "searched-string"
对于Linux:
grep -r "searched-string" .
在UNIX服务器上查找文件
find . -type f -name file_name
在LINUX服务器上查找文件
find . -name file_name
这是在我当前的机器上(windows 7上的git bash)适用于我的案例的例子:
find ./ -type f -iname "*.cs" -print0 | xargs -0 grep "content pattern"
对于带有空格的路径,我总是忘记-print0和-0。
编辑:我的首选工具现在是ripgrep:https://github.com/BurntSushi/ripgrep/releases . 它真的很快,并且有更好的默认值(比如默认情况下的递归)。与我的原始答案相同的示例,但使用了ripgrep:rg-g“*.cs”“content pattern”