Jason Jason - 5 months ago 26
Linux Question

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.

I want to grep just

.h
and
.cpp
for now, but maybe a few others in the future.

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@domain.com


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

Answer

Just use the --include parameter, like this:

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

that should do what you want.