所有目录中的grep [英] grep in all directories
问题描述
我有一个名为XYZ的目录,里面有目录ABC,DEF,GHI.我想在所有目录中的所有* .c中搜索模式'writeText'(即XYZ,XYZ/ABC,XYZ/DEF和XYZ/GHI)我可以使用什么grep命令?
I have a directory named XYZ which has directories ABC, DEF, GHI inside it. I want to search for a pattern 'writeText' in all *.c in all directories (i.e XYZ, XYZ/ABC, XYZ/DEF and XYZ/GHI) What grep command can I use?
如果我只想在XYZ,XYZ/ABC,XYZ/GHI中搜索而不在XYZ/DEF中搜索,我可以使用什么grep命令?
Also if I want to search only in XYZ, XYZ/ABC, XYZ/GHI and not XYZ/DEF, what grep command can I use?
谢谢!
推荐答案
grep -R --include="*.c" --exclude-dir={DEF} writeFile /path/to/XYZ
-
-R
表示递归,因此它将进入您要遍历的目录的子目录中 -
-include ="*.c"
的意思是查找以.c
结尾的文件" -
-exclude-dir = {DEF}
的意思是排除名为DEF
的目录.如果要排除多个目录,请执行以下操作:-exclude-dir = {DEF,GBA,XYZ}
-
writeFile
是您要抓取的模式 -
/path/to/XYZ
是您要grep通过的目录的路径. -R
means recursive, so it will go into subdirectories of the directory you're grepping through--include="*.c"
means "look for files ending in.c
"--exclude-dir={DEF}
means "exclude directories namedDEF
. If you want to exclude multiple directories, do this:--exclude-dir={DEF,GBA,XYZ}
writeFile
is the pattern you're grepping for/path/to/XYZ
is the path to the directory you want to grep through.
请注意,这些标志适用于GNU grep
,如果您使用的是BSD/SysV/AIX grep
,则可能会有所不同.如果您使用的是Linux/GNU grep utils,那么应该没问题.
Note that these flags apply to GNU grep
, might be different if you're using BSD/SysV/AIX grep
. If you're using Linux/GNU grep utils you should be fine.
这篇关于所有目录中的grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!