如何递归地grep所有目录和子目录?
find . | xargs grep "texthere" *
如何递归地grep所有目录和子目录?
find . | xargs grep "texthere" *
当前回答
对于.gz文件,递归扫描所有文件和目录更改文件类型或放置*
find . -name \*.gz -print0 | xargs -0 zgrep "STRING"
其他回答
注意这个发现-当查找匹配的文件太多时,键入f | xargs grep任何类型的解决方案都会遇到“Argument list to long”错误。
最好的选择是grep-r,但如果不可用,请使用find-键入f-exec grep-H whatever{}\;相反
在IBM AIX Server(操作系统版本:AIX 5.2)中,使用:
find ./ -type f -print -exec grep -n -i "stringYouWannaFind" {} \;
这将打印出文件中的路径/文件名和相对行号,如:
./inc/xxxx_x.h
2865:/**描述:stringYouWanna查找*/
无论如何,这对我来说很有用:)
把我的两分钱扔在这里。正如其他人已经提到的那样,grep-r并不适用于所有平台。这听起来可能很傻,但我总是使用git。
git grep "texthere"
即使目录没有暂存,我也只是暂存并使用git grep。
如果您只想跟踪实际的目录,而不是符号链接,
grep -r "thingToBeFound" directory
如果您希望遵循符号链接以及实际目录(注意无限递归),
grep -R "thing to be found" directory
由于您正在尝试递归grep,以下选项可能对您也有用:
-H: outputs the filename with the line
-n: outputs the line number in the file
因此,如果您希望在当前目录或任何子目录中查找包含达斯·维德的所有文件,并捕获文件名和行号,但不希望递归遵循符号链接,则命令如下
grep -rnH "Darth Vader" .
如果你想在目录中找到所有提到的单词cat
/home/adam/Desktop/TomAndJerry
并且您当前在目录中
/home/adam/Desktop/WorldDominationPlot
如果您希望捕获字符串“cats”的任何实例的文件名而不是行号,并且希望递归在找到符号链接时遵循符号链接,您可以运行以下任一操作
grep -RH "cats" ../TomAndJerry #relative directory
grep -RH "cats" /home/adam/Desktop/TomAndJerry #absolute directory
资料来源:
运行“grep--help”
对符号链接的简短介绍,对于任何阅读此答案并被我提到的符号链接所迷惑的人:https://www.nixtutor.com/freebsd/understanding-symbolic-links/
也:
find ./ -type f -print0 | xargs -0 grep "foo"
但grep-r是更好的答案。