How do I recursively grep through all sub-directories and find files containing given text / string or words?
You can use the -r option to recursively grep through all sub-directories and find text. The syntax is as follows:
grep -r "text" /path/to/di
In this example, search for an IP 192.168.1.254 in through all /etc/ and all its sub-directories:
grep -r "192.168.1.254" /etc/
Search in any case:
grep -ri "letters" /home/vivek/data
Use the -l switch to display only the names of files in which the text occurs:
grep -lri "foo" /data
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop










![HowTo: Use grep Command In Linux / UNIX [ Examples ]](http://s13.cyberciti.org/images/shared/rp/3/10.jpg)
![Linux Copy File Command [ cp Command Examples ]](http://s13.cyberciti.org/images/shared/rp/3/11.jpg)

{ 2 comments… read them below or add one }
Grep does allow recursive search, but only if you don’t specify the files you are looking for. For example:
grep -r ‘ActiveServer’ . works, but outputs a lot of noise (like log and binary files)
grep -r ‘ActiveServer’ *.java will tell something like:
“zsh: no matches found: *.java”
A “quick and dirty” way to solve the problem:
grep -r ‘ActiveServer’ . | grep ‘.java:’
This still might output some non-java entries.
But this doesn’t ():
grep -rl ‘ActiveServer’ . | grep ‘.java’
I usually run this command and a separate terminal tab (or window).
When I want to grep over files with a specific extension in a file tree I do it combining it with “find”:
$ find . -name \*.java | xargs grep ‘ActiveServer’