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?

LinuxBashUnixGrep

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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

grep hello *.h *.cc

Solution 3 - Linux

To search in current directory recursively:

grep -r 'myString' .

Solution 4 - Linux

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

Check the manual pages for find and xargs for details.

Solution 5 - Linux

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)$'`

Solution 6 - Linux

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.

Solution 7 - Linux

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.

Solution 8 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstackoverflowView Question on Stackoverflow
Solution 1 - LinuxstackoverflowView Answer on Stackoverflow
Solution 2 - LinuxDonald MinerView Answer on Stackoverflow
Solution 3 - LinuxAB AbhiView Answer on Stackoverflow
Solution 4 - LinuxNoufal IbrahimView Answer on Stackoverflow
Solution 5 - LinuxJackView Answer on Stackoverflow
Solution 6 - LinuxJonathan LefflerView Answer on Stackoverflow
Solution 7 - Linuxglenn jackmanView Answer on Stackoverflow
Solution 8 - Linuxuser1733951View Answer on Stackoverflow