grep

Keith Bloom akbloom at aplcen.apl.jhu.edu
Mon Apr 15 14:21:00 AEST 1991


mmoore%hellgate.utah.edu at cs.utah.edu (Michael Moore) writes:


>	Does anyone know if there is an easy way to recursively search for a 
>pattern down the entire file tree of a directory?  

If your system has xargs, you could try:

find . -name '*' -print | xargs grep pattern

If you have a huge directory tree with thousands of files in it, this
may not work.

If you don't have xargs, there's:

find . -name '*' -print -exec grep pattern {} \;

but this is more cumbersome, because it will print the names of all 
your files, whether they contain the pattern or not.  (I assume you
want to know the name of the file that 'pattern' is in.)

In general, 'find' is usually your best bet for recursive operations
like the one you have in mind.

(PS: both methods work as stated under Ultrix.  They ought to work the
same on any reasonable Unix system, but there's no guarantee.)



More information about the Comp.unix.questions mailing list