How do I use grep to search the current directory for all files having the a string "hello" yet display only .h and .cc files? How do I use grep to search the current directory for all files having the a string "hello" yet display only .h and .cc files? unix unix

How do I use grep to search the current directory for all files having the a string "hello" yet display only .h and .cc files?


grep -r --include=*.{cc,h} "hello" .

This reads: search recursively (in all sub directories also) for all .cc OR .h files that contain "hello" at this . (current) directory

From another stackoverflow question


You can pass in wildcards in instead of specifying file names or using stdin.

grep hello *.h *.cc


find . -name \*.cc -print0 -or -name \*.h -print0 | xargs -0 grep "hello".

Check the manual pages for find and xargs for details.