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

find . | xargs grep "texthere" *

当前回答

注意这个发现-当查找匹配的文件太多时,键入f | xargs grep任何类型的解决方案都会遇到“Argument list to long”错误。

最好的选择是grep-r,但如果不可用,请使用find-键入f-exec grep-H whatever{}\;相反

其他回答

下面是在Unix和Linux环境中递归搜索字符串的命令。

对于UNIX命令是:

find . -name "string to be searched" -exec grep "text" "{}" \;

对于Linux,命令是:

grep -r "string to be searched" .

这应该是有效的:

grep -R "texthere" *

这是在我当前的机器上(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”

注意这个发现-当查找匹配的文件太多时,键入f | xargs grep任何类型的解决方案都会遇到“Argument list to long”错误。

最好的选择是grep-r,但如果不可用,请使用find-键入f-exec grep-H whatever{}\;相反

grep-r“texthere”。(通知期结束)

(^信用:https://stackoverflow.com/a/1987928/1438029)


澄清:

grep-r“texthere”/(递归地grep所有目录和子目录)

grep-r“texthere”。(递归地grep这些目录和子目录)

grep递归

grep[选项]模式[文件…][选项]-R、 -R,--递归递归地读取每个目录下的所有文件。这相当于-d recurse或--directories=recurse选项。http://linuxcommand.org/man_pages/grep1.html

grep帮助

$grep--帮助

$ grep --help |grep recursive
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive

选择

确认(http://beyondgrep.com/)

银(http://github.com/ggreer/the_silver_searcher)