[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: find | egrep



zdrysdal@diagnostic.co.nz (zdrysdal@diagnostic.co.nz) wrote:

> I need to be able
> to use find and egrep to scan a directory which has more than 3000 files in
> it.

find . -type f -print | xargs egrep pattern /dev/null


Enrico Zini (zinie@CS.UniBO.IT) wrote:

> find <options> | xargs egrep "pattern"

... except that you've omitted /dev/null (see below).

> find <options> | while read a; do egrep "pattern" $a; done
> [...]
> find <options> -exec egrep "pattern" {} \;

These two have one major problem: egrep is only getting one filename, so
it will not print that filename.  Thus, you won't know which file matched
the pattern.

That's why the command I gave (with /dev/null) is best.  It passes
multiple files to egrep at a time (you're guaranteed to get at least
one per call with xargs, so adding /dev/null gives you at least two
filenames per call to egrep -- so egrep will always print out the name
of the file which matches).

Passing multiple files at a time to egrep is also more efficient --
the -exec version may be significantly slower.

-- 
Greg Wooledge                    | Distributed.NET http://www.distributed.net/
wooledge@kellnet.com             | because a CPU is a terrible thing to waste.
http://www.kellnet.com/wooledge/ |

Attachment: pgp9O8ZscZyea.pgp
Description: PGP signature


Reply to: