linux grep h文件或目录,linux - grep,但只有某些文件扩展名

linux - grep,但只有某些文件扩展名

我正在编写一些脚本到grep某些目录,但这些目录包含各种文件类型。

我现在想grep只是.h和.cpp,但未来可能还有其他几个。

到目前为止我有:

{ 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

任何人都可以告诉我如何添加特定的文件扩展名?

11个解决方案

986 votes

只需使用--include=\*.${file_extension}参数,如下所示:

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

应该做你想要的。

语法说明:

--include=\*.${file_extension} - 递归搜索

--include=\*.${file_extension} - 不区分大小写的搜索

--include=\*.${file_extension} - 仅搜索与扩展名或文件模式匹配的文件

Nelson answered 2018-12-23T02:13:01Z

198 votes

其中一些答案似乎语法太重,或者它们在我的Debian服务器上产生了问题。 这对我很有用:

PHP革命:如何在Linux中Grep文件,但只有某些文件扩展名?

即:

grep -r --include=\*.txt 'searchterm' ./

...或不区分大小写的版本......

grep -r -i --include=\*.txt 'searchterm' ./

./:命令

./:递归地

./:ignore-case

./:all * .txt:文本文件(用\文件转义,以防你在文件名中有一个带星号的目录)

./:搜索什么

./:从当前目录开始。

HoldOffHunger answered 2018-12-23T02:14:01Z

44 votes

怎么样:

find . -name '*.h' -o -name '*.cpp' -exec grep "CP_Image" {} \; -print

Amir Afghani answered 2018-12-23T02:14:21Z

40 votes

grep -rnw "some thing to grep" --include=*.{module,inc,php,js,css,html,htm} ./

Yuri Malov answered 2018-12-23T02:14:36Z

13 votes

HP和Sun服务器上没有-r选项,这种方式适用于我的HP服务器

find . -name "*.c" | xargs grep -i "my great text"

-i用于不区分大小写的字符串搜索

Sam B answered 2018-12-23T02:15:01Z

8 votes

由于这是找文件的问题,让我们使用find!

使用GNU find,您可以使用grep选项在扩展名为--include或.cpp的目录树中查找这些文件:

find -type f -regex ".*\.\(h\|cpp\)"

# ^^^^^^^^^^^^^^^^^^^^^^^

然后,只需在每个结果上执行grep:

find -type f -regex ".*\.\(h\|cpp\)" -exec grep "your pattern" {} +

如果你没有这种发现分布,你必须使用像Amir Afghani这样的方法,使用grep连接选项(名称以grep或--include结尾):

find -type f \( -name '*.h' -o -name '*.cpp' \) -exec grep "your pattern" {} +

# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

如果您真的想使用grep,请按照指示的语法--include:

grep "your pattern" -r --include=*.{cpp,h}

# ^^^^^^^^^^^^^^^^^^^

fedorqui answered 2018-12-23T02:15:39Z

6 votes

最简单的方法是

find . -type f -name '*.extension' | xargs grep -i string

user3640130 answered 2018-12-23T02:15:59Z

3 votes

我知道这个问题有点陈旧,但我想分享我通常用来查找.c和.h文件的方法:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -H "#include"

或者如果您还需要行号:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -nH "#include"

Tom answered 2018-12-23T02:16:24Z

2 votes

ag(银色搜索者)有相当简单的语法

-G --file-search-regex PATTERN

Only search files whose names match PATTERN.

所以

ag -G *.h -G *.cpp CP_Image

Eldamir answered 2018-12-23T02:16:48Z

1 votes

以下答案很好。

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

但可以更新为:

grep -r -i --include \*.{h,cpp} CP_Image ~/path[12345] | mailx -s GREP email@domain.com

哪个可以更简单。

hao answered 2018-12-23T02:17:16Z

0 votes

应该为每个“-o -name”写“-exec grep”

find . -name '*.h' -exec grep -Hn "CP_Image" {} \; -o -name '*.cpp' -exec grep -Hn "CP_Image" {} \;

或者按()分组

find . \( -name '*.h' -o -name '*.cpp' \) -exec grep -Hn "CP_Image" {} \;

选项'-Hn'显示文件名和行。

where23 answered 2018-12-23T02:17:45Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值