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?
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)$'`
Related videos on Youtube
stackoverflow
Updated on July 08, 2022Comments
-
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 about 12 yearsI need all sub directories also
-
Donald Miner about 12 yearsAdding
-R
doesn't work unfortunately. It expands the*
before going recursively. -
Donald Miner about 12 yearsNoufal's answer is what you are looking for if you need subdirectories
-
Jonathan Leffler about 12 yearsYou 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 about 12 yearsThanks for noticing that. I've edited the answer to "fix" it.
-
jordanm about 12 yearsYou can also use the
find -exec +
syntax instead of xargs -
Jonathan Leffler about 12 years@jordanm: Yes:
-exec grep "hello" {} +
where the pair of braces represents the file name(s). Good suggestion. -
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 over 10 years
$ grep -R hello *
# find from all the word contain hello (including sub directory) -
Andrew Swift over 9 yearsI 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 over 8 yearsThis doesn't display only .h or .cc files and it is useful to explain which options you used and why.
-
fedorqui over 7 yearsnote you can also say
find ... -exec grep "hello" {} +
-
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 over 7 yearsIn 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 about 7 years
grep -r --include=*.{java,py} "hello" .
-
xApple almost 4 yearsFor some reason this doesn't work on macOS, even when using the GNU grep utility.