Unix and Linux: How to Grep Recursively?

Fig.01: Unix and Linux: How to Grep Recursively?

Fig.01: Unix and Linux: How to Grep Recursively?

The syntax is:

## Basic syntax ##
cd /to/dir/
grep -r "pattern-to-search" *
grep -r "foo" .
 
## Only look for *.pl files ##
grep -r --include "*.pl" "bar" .
 
## Search all but exclude all *.py files ##
grep -r --exclude "*.py" "text-to-search-here" .
 
## If grep -r syntax not supported try ##
find /dir/to/search/ -type f | xargs grep "bar"
 
## Solaris / HP-UX / AIX specific example etc ##
find . -type f -exec grep -n "bar" {} \; -print
 
## Apple OS X specific example ##
find /DIR/TO/SEARCH/ -iname "*" -type f -print0 | xargs -0 grep -H "foo"

For more info read grep and find command man pages.

Examples and usage: Linux / UNIX Recursively Search All Files For A String