Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

People also ask

How do I use grep to search all files in a directory?

To Search Subdirectories To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.

How do I use Linux grep to find a directory?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do you search for a word in all files in a directory in Linux?

Where the -R option tells grep to read all files under each directory, recursively, following symbolic links only if they are on the command line and option -w instructs it to select only those lines containing matches that form whole words, and -e is used to specify the string (pattern) to be searched.


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


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

grep hello *.h *.cc

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

Check the manual pages for find and xargs for details.


To search in current directory recursively:

grep -r 'myString' .