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

查看:19
本文介绍了我怎样才能递归地 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

这应该可以满足您的需求.

That should do what you want.

HoldOffHunger 的回答如下:

  • grep:命令

-r:递归

-i:忽略大小写

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

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

--include *.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天全站免登陆