我如何递归grep,但只能在具有某些扩展名的文件中? [英] How can I grep recursively, but only in files with certain extensions?

查看:46
本文介绍了我如何递归grep,但只能在具有某些扩展名的文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a script to grep certain directories:

{ 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 ?

How can I limit results only to extensions .h and .cpp?

推荐答案

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

Just use the --include parameter, like this:

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

那应该做你想要的.

要从

  • grep :命令

    -r :递归

    -i :忽略大小写

    -n :每条输出行在文件中均以其相对行号开头

    -n: each output line is preceded by its relative line number in the file

    -包括\ *.cpp :所有* .cpp:C ++文件(如果文件名中带有星号的目录,请使用\转义)

    --include \*.cpp: all *.cpp: C++ files (escape with \ just in case you have a directory with asterisks in the filenames)

    ./:从当前目录开始.

    这篇关于我如何递归grep,但只能在具有某些扩展名的文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆