Sunday, March 04, 2007

Perform Recursive Pattern Searches on Files

As a UNIX systems administrator, I am frequently asked how to find a particular pattern or string in a file in which the location of the file is unknown. This is a nifty recursive way of doing that. I routinely use it on the command line.

# /usr/bin/find /etc –type f | xargs grep –i PATTERN

These types of searches can take awhile depending on which directories you are searching.

If you have an alternate method, please share it.

Here's a refined method given by ux-admin
ux-admin said...

Tip: to speed the search up, use "-depth", and a case-sensitive `grep`, like so:

find /a/path/ -depth -type f -print | xargs grep the-string-you-are-looking-for

6 comments:

ux-admin said...

Tip: to speed the search up, use "-depth", and a case-sensitive `grep`, like so:

find /a/path/ -depth -type f -print | xargs grep the-string-you-are-looking-for

esofthub said...

Appreciate the tip ux-admin

wad said...

I've used this to varying degrees of success.

find /a/path -depth -type f -exec grep -l (text you want) {} \;

xargs sometimes barfs if you pass it too many files...

esofthub said...

wad, I'll try your method.

Anonymous said...

grep -r

Anonymous said...

not all grep are created equal. some do not have the -r switch