190

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?

stackoverflow
  • 18,348
  • 50
  • 129
  • 196

8 Answers8

200
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

GypsyCosmonaut
  • 661
  • 12
  • 17
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
  • 19
    In 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
  • 1
    For some reason this doesn't work on macOS, even when using the GNU grep utility. – xApple Jul 07 '20 at 15:45
58

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

grep hello *.h *.cc
Donald Miner
  • 38,889
  • 8
  • 95
  • 118
31

To search in current directory recursively:

grep -r 'myString' .
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
21

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

Check the manual pages for find and xargs for details.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
8

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)$'`
Jack
  • 1,477
  • 15
  • 20
6

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2
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.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 4
    I 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
-2

The simplest way : grep -Ril "Your text" /

  • 2
    This 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