How do I use grep to search the current directory for any and all files containing the string "hello" and display only .h and .cc files?
8 Answers
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

- 661
- 12
- 17

- 18,348
- 50
- 129
- 196
-
19In the original post you said "current directory" and nothing about needing it recursively or in sub directories (I realize you mentioned it in a later post). For those interested in how to do it only in the current directory, it's `grep -si "hello" --include=*.{cc,h} ./* ./.*`(searches hidden files as well). Here's a generic version to search for a string in all/hidden files: `grep -s "hello" * .*` . Maybe most *generally* useful (and simplest), this searches all non-hidden files for "hello": `grep -si "hello" ./*` Thanks to this post: http://askubuntu.com/a/777456 – jbobbins Sep 28 '16 at 03:31
-
1`grep -r --include=*.{java,py} "hello" .` – Feb 27 '17 at 01:50
-
1For some reason this doesn't work on macOS, even when using the GNU grep utility. – xApple Jul 07 '20 at 15:45
You can pass in wildcards in instead of specifying file names or using stdin.
grep hello *.h *.cc

- 38,889
- 8
- 95
- 118
-
-
Adding `-R` doesn't work unfortunately. It expands the `*` before going recursively. – Donald Miner Feb 09 '12 at 19:23
-
Noufal's answer is what you are looking for if you need subdirectories – Donald Miner Feb 09 '12 at 19:23
-
9@Jammin You should be clear about that when asking the question. This answer is the "correct" answer to your stated question. – jordanm Feb 09 '12 at 21:12
-
1`$ grep -R hello *` # find from all the word contain hello (including sub directory) – Aug 17 '13 at 14:57
To search in current directory recursively:
grep -r 'myString' .

- 69,473
- 35
- 181
- 253

- 2,939
- 1
- 25
- 34
find . -name \*.cc -print0 -or -name \*.h -print0 | xargs -0 grep "hello"
.
Check the manual pages for find
and xargs
for details.

- 71,383
- 13
- 135
- 169
-
1You have to have a `-print0` after the `'*.cc'` as otherwise you don't have an action for that part of the search. Or you have to insert parentheses in there: `find . \( -name '*.cc' -or -name '*.h' \) -print0`. – Jonathan Leffler Feb 09 '12 at 19:26
-
-
3
-
@jordanm: Yes: `-exec grep "hello" {} +` where the pair of braces represents the file name(s). Good suggestion. – Jonathan Leffler Feb 09 '12 at 20:11
If I read your question carefully, you ask to "grep to search the current directory for any and all files containing the string "hello" and display only .h and .cc files". So to meet your precise requirements here is my submission:
This displays the file names:
grep -lR hello * | egrep '(cc|h)$'
...and this display the file names and contents:
grep hello `grep -lR hello * | egrep '(cc|h)$'`

- 1,477
- 15
- 20
If you need a recursive search, you have a variety of options. You should consider ack
.
Failing that, if you have GNU find
and xargs
:
find . -name '*.cc' -print0 -o -name '*.h' -print0 | xargs -0 grep hello /dev/null
The use of /dev/null
ensures you get file names printed; the -print0
and -0
deals with file names containing spaces (newlines, etc).
If you don't have obstreperous names (with spaces etc), you can use:
find . -name '*.*[ch]' -print | xargs grep hello /dev/null
This might pick up a few names you didn't intend, because the pattern match is fuzzier (but simpler), but otherwise works. And it works with non-GNU versions of find
and xargs
.

- 730,956
- 141
- 904
- 1,278
grep -l hello **/*.{h,cc}
You might want to shopt -s nullglob
to avoid error messages if there are no .h or no .cc files.

- 238,783
- 38
- 220
- 352
-
4I know how to use google etc. I asked it here because several answers have "-l" and it's nice to have all the information in one place. – Andy Swift Jan 16 '15 at 11:11
-
-l (Lowercase "el") Suppresses normal output. Prints the name of each input file from which output would normally have been printed. The scanning/searching will stop on the first match. – Rex Charles Sep 15 '16 at 21:06
The simplest way : grep -Ril "Your text" /

- 43
- 4
-
2This doesn't display only .h or .cc files and it is useful to explain which options you used and why. – kon psych Dec 13 '15 at 07:36