I'm using Debian Linux as my development workstation. I would like to search a directory called ~/projects/ recursively for "foo" word only for *.txt files. How do I search all text files in ~/projects/ for "foo" word using grep command?
The grep command supports recursive file pattern
grep -R "pattern" /path/to/dir/
To limit your search for *.txt, try passing the --include option to grep command
Syntax and examples for --include option
The syntax is:
grep -R --include=GLOB "pattern" /path/to/dir grep -R --include="*.txt" "pattern" /path/to/dir grep -R --include="*.txt" "foo" ~/projects/
You can include files whose base name matches GLOB using wildcard matching. A file-name glob can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character literally. You can ignore case distinctions in both the PATTERN and the input files with -i optoon i.e. case-insensitive search. In this following example, search for all *.py, *.pl, and *.sh files for "main" word in my /raid6/projects/sysmanagement/ directory:
grep --color -Ri --include="*.py" --include="*.sh" --include="*.pl" "main" /raid6/projects/sysmanagement/
OR
grep --color -Ri --include=*.{py,pl,sh} "main" /raid6/projects/sysmanagement/
OR a safer option would be (note --color removed and * replaced with \*):
grep -Ri --include=\*.{py,pl,sh} "main" /raid6/projects/sysmanagement/
The --include option provides you the following advantages:
- Speed up the search.
- Only match given file pattern.
- Do not search for binary files such as compiled files or image files. In other words only look for *.txt or *.py file patterns and so on.
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/22.jpg)










{ 8 comments… read them below or add one }
this isn’t portable and includes lots of annoying GNUisms.
It is better to use find . -name \*.txt | xargs grep
It’s really a awful way to use grep that I havn’t seen. I also use find . -name \*.txt | xargs grep before.
This matches file names; it doesn’t use globbing:
grep -R –include=GLOB “pattern” /path/to/dir
In other words, it will include dot files, which globbing does not.
How about enabled globstar(which most ppl I know have anyway) and then grep “foo” /path/**.txt ? Works at least in the richer shells like bash or zsh.
xargs? ewwwwggg. What real men do is:
find /some/path -type f -name *.txt -exec grep “pattern” {} +
Zsh Only
grep “pattern” **/*.txt
May not work if the number of matching files are too many.
Balakrishnan, ** also works in bash (version 4) with the globstar option.
Real man just use:
ack –text “pattern”