Disclaimer: You are looking at a post I wrote some time ago. The information and opinions contained within may be outdated and may differ from my current views. Please proceed accordingly.

Recursive Linux "Find Files Containing"

Apr 30, 2003 4:27 PM
Tags: bash, linux, programming, unix

This makes use of the regrep script I mentioned earlier.

regrep "whatever" | awk -F":" {'print $1'} | uniq

Prints out a list of files containing whatever pattern you put. Kinda equivalent to Windows' "find files containing" search.


Comments: Recursive Linux "Find Files Containing"

awesome script, thanks alot!

Posted by: Todd on October 31, 2003 3:55 PM | permalink

No problem.

I probably should combine the bash and perl scripts into a single (Perl?) script, and make "show file names only" a command-line option instead of piping it through awk.

For now, though, this suits me just fine.

Posted by: Joe Grossberg on October 31, 2003 3:58 PM | permalink

Seems like a lot of trouble -- why don't you simply:

grep -lir "some text" *

or,

find /path -name *.txt | grep -lir "some text" *

??

Posted by: Ken on January 29, 2007 3:50 PM | permalink

Yes, and in the 3.5 years since I wrote that post, I've learned about the "-l" flag :)

Posted by: Joe Grossberg on January 29, 2007 4:20 PM | permalink

just what i was looking for...
thanks joe
thanks ken

Posted by: borja on March 29, 2007 12:49 PM | permalink

grep -lir is my hero! thanks....

Posted by: Rnast on June 27, 2007 6:04 PM | permalink

So to add a little to Ken's comment

find /path -name *.txt | grep -lir "some text" *

That wouldn't exactly work how it is written.

I wanted to search everywhere, but exclude the /dev , so I did

find * | grep -vi dev | xargs grep -li "some text"

find * returns all file names
grep -v dev returns everything that doesn't have dev (the v inverts the search, i makes it case insensitive)

The reason Ken's wouldn't work is the xargs, the paths for grep to search needed to be handed in as args rather than through the pipe, so his with the * would just search everything anyway.

I also took the r off the last grep so it wouldn't go recursive and look at things not specifically returned.

Posted by: Doug on December 20, 2007 5:31 AM | permalink

Here is an alternative (will find files within subdirectories containing the value that you provide):

find . -name *.extension -exec grep -i -H -n 'texttofind' {} \;

Enjoy!

Posted by: Ken on May 14, 2008 11:37 AM | permalink

No more comments! Either someone has violated Godwin's Law, I'm tired of the discussion or, most likely, the ten-week window has closed. You can, however, contact me through email.