Since this is a matter of finding files, let's use find
!
Using GNU find you can use the -regex
option to find those files in the tree of directories whose extension is either .h
or .cpp
:
find -type f -regex ".*\.\(h\|cpp\)"
# ^^^^^^^^^^^^^^^^^^^^^^^
Then, it is just a matter of executing grep
on each of its results:
find -type f -regex ".*\.\(h\|cpp\)" -exec grep "your pattern" {} +
If you don't have this distribution of find you have to use an approach like Amir Afghani's, using -o
to concatenate options (the name is either ending with .h
or with .cpp
):
find -type f \( -name '*.h' -o -name '*.cpp' \) -exec grep "your pattern" {} +
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
And if you really want to use grep
, follow the syntax indicated to --include
:
grep "your pattern" -r --include=*.{cpp,h}
# ^^^^^^^^^^^^^^^^^^^
grep -r -i CP_Image ~/path1/*.{h,cpp}
? – user529758ag -i CP_Image ~/path[1-5] | mailx -s GREP [email protected]
. Job done. – Johnsyweb-r
togrep
to have it search for files as that breaks the UNIX mantra of having tools that "do one thing and do it well". There's a perfectly good tool for finding files with a VERY obvious name. – Ed Morton