小编典典

我怎样才能递归地grep,但只能在具有某些扩展名的文件中?

all

我正在编写grep某些目录的脚本:

{ grep -r -i CP_Image ~/path1/;
grep -r -i CP_Image ~/path2/;
grep -r -i CP_Image ~/path3/;
grep -r -i CP_Image ~/path4/;
grep -r -i CP_Image ~/path5/; }
| mailx -s GREP email@domain.com

如何将结果限制为仅扩展 .h.cpp


阅读 169

收藏
2022-02-25

共1个答案

小编典典

只需使用--include参数,如下所示:

grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.com

那应该做你想做的事。

从下面的答案中获取解释:

  • grep: 命令

  • -r: 递归

  • -i: 忽略大小写

  • -n:每个输出行前面都有其在文件中的相对行号

  • --include \*.cpp: all *.cpp: C++ 文件(用 \ 转义,以防万一你有一个文件名中带有星号的目录)

  • ./: 从当前目录开始。

2022-02-25