Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

linux - grep,但只有特定的文件扩展名(grep, but only certain file extensions)

I am working on writing some scripts to grep certain directories, but these directories contain all sorts of file types.

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

I want to grep just .h and .cpp for now, but maybe a few others in the future.

(我想grep只是.h.cpp现在,但也许其他几个人的未来。)

So far I have:

(到目前为止,我有:)

{ 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 protected]

Can anyone show me how I would now add just the specific file extensions?

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

  ask by Jason translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just use the --include parameter, like this:

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

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

that should do what you want.

(应该做你想要的。)

Syntax notes:

(语法说明:)

  • -r - search recursively

    (-r - 递归搜索)

  • -i - case- insensitive search

    (-i - 不区分大小写的搜索)

  • --include=\*.${file_extension} - search files that match the extension(s) or file pattern only

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...