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?

299,456

Solution 1

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

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

grep hello *.h *.cc

Solution 3

To search in current directory recursively:

grep -r 'myString' .

Solution 4

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

Check the manual pages for find and xargs for details.

Solution 5

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)$'`
Share:
299,456

Related videos on Youtube

stackoverflow
Author by

stackoverflow

Updated on July 08, 2022

Comments

  • stackoverflow
    stackoverflow almost 2 years

    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
    stackoverflow about 12 years
    I need all sub directories also
  • Donald Miner
    Donald Miner about 12 years
    Adding -R doesn't work unfortunately. It expands the * before going recursively.
  • Donald Miner
    Donald Miner about 12 years
    Noufal's answer is what you are looking for if you need subdirectories
  • Jonathan Leffler
    Jonathan Leffler about 12 years
    You 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.
  • Noufal Ibrahim
    Noufal Ibrahim about 12 years
    Thanks for noticing that. I've edited the answer to "fix" it.
  • jordanm
    jordanm about 12 years
    You can also use the find -exec + syntax instead of xargs
  • Jonathan Leffler
    Jonathan Leffler about 12 years
    @jordanm: Yes: -exec grep "hello" {} + where the pair of braces represents the file name(s). Good suggestion.
  • jordanm
    jordanm about 12 years
    @Jammin You should be clear about that when asking the question. This answer is the "correct" answer to your stated question.
  • Admin
    Admin over 10 years
    $ grep -R hello * # find from all the word contain hello (including sub directory)
  • Andrew Swift
    Andrew Swift over 9 years
    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.
  • kon psych
    kon psych over 8 years
    This doesn't display only .h or .cc files and it is useful to explain which options you used and why.
  • fedorqui
    fedorqui over 7 years
    note you can also say find ... -exec grep "hello" {} +
  • Rex Charles
    Rex Charles over 7 years
    -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.
  • jbobbins
    jbobbins over 7 years
    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: askubuntu.com/a/777456
  • Admin
    Admin about 7 years
    grep -r --include=*.{java,py} "hello" .
  • xApple
    xApple almost 4 years
    For some reason this doesn't work on macOS, even when using the GNU grep utility.