How to search a directory recursively for specific files containing a text string in linux / unix

Posted on 2011-10-13
Medium Priority
Last Modified: 2012-05-12

I would like a command to search a directory recursivly for all .txt files containing the string "string".  I have tried to accomplish this using grep, but it is searching more than just .txt files.  Here is the command I have tried, but its not working correctly:

grep -lr "string" /root/volumes/ *txt
Question by:electricd7

Expert Comment

ID: 36964583

I would go first for "find".

I would search the files with the command find and then use xargs to grep the string you want.

I attached a code snippet.

Good Luck!
find /path/ -type f -name "*.txt" | xargs grep "string"

Open in new window

LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 36964687
Hi, I use a script called "mgrep" for this:

for a in `find . -type f`; do file -bi $a | grep "^text" 2>&1 >/dev/null && grep -l "$1" $a; done;

Open in new window

Searches from current folder in all subfolders for string given as a parameter, like this:

mgrep <string>

It will print all text files that match
LVL 48

Expert Comment

ID: 36964939
Problem is the space, so your grep is searching

/root/volumes AND *txt

should be
grep -lr "string" /root/volumes/*txt

Open in new window


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 36966681
#grep -i string */*.txt

If you want to put the results on a file add >myoutput.log at the end of the command...

#grep -i string */*.txt>myoutput.log

-hope helps-
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 36967147
find . -type f | xargs grep -l "string"

Open in new window

LVL 20

Accepted Solution

simon3270 earned 2000 total points
ID: 36968155
There are two options - you only want to search files with the ".txt" extension, or you want to search any files that have text in (so don't check binary files).

For the first:
find /root/volumes -name "*.txt" -print0 | xargs -0 grep "string" /dev/null

Open in new window

(the "-print-" and "-0" options are to support filenames with spaces in them)

To search *any* text files
find /root/volumes -type f | xargs file | awk -F: '$2 ~ /text/{print $1}' | xargs grep "string" /dev/null

Open in new window

By the way, the extra /dev/null after the grep command is because grep only prints the filename for found strings if there is more than one filename after the grep.  If you have
    grep "string" file1 file2
grep will preced each "string" line with the filename (here, file1 or file2).  If you have:
    grep "string" file1
it will just print the text it finds, without a preceding filename.  This might happen if, for example, there *is* only one text file found, or if xargs splits the list of files up into chunks, and the last chunk contains just one name.

One problem with the second command is that it will fail to handle filenames with spaces. If you need that, there is a fix I have for the awk command - let me know if you want it.
LVL 20

Expert Comment

ID: 36968560
Thanks for the points!

By the way, a version of the second command which handles filenames with spaces in is:
find /root/volumes -type f -print0 | xargs -0 file | awk -F: '$2 ~ /text/{printf "%s%c",$1,0}' | xargs -0 grep "string" /dev/null

Open in new window


Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question