Recursion with grep?

Jan Grant Jan.Grant at bristol.ac.uk
Fri Nov 14 00:56:36 PST 2003


On Thu, 13 Nov 2003, Francisco Reyes wrote:

> On Fri, 14 Nov 2003, JacobRhoden wrote:
>
> > No need to hack grep plese! just use -R (it appears the man page does not
> > document the -R function, but you need to use -R in grep for it to recurse.
>
> -R == -r
>
> That was mentioned in the previous emails. It only recurses directories.
> It will not work with a filemask.
>
> You can't say
> grep -r <string> *.c
>
> You can only say
> grep -r <string> <directory>

That's correct, but the Unix Way is* to use small tools that do one job
well* and compose them for the desired effect. Someone's already given
you an invocation of find and xargs that (almost) does what you want:

find . -type f -name \*.c -print0 | xargs -0 grep -h searchstring

If you like, wrap this up with a script. Call the script "grep" and
stick it closer to the start of your path than the system one.

Even the win32 shell is more capable of command composition these days.
Chucking as much disparate functionality as possible into each tool
is a poor tactic because you wind up with every tool being
indistinguishable from its peers, modulo the totally incompatible and
irregular interface :-)

G'luck,
jan

* allegedly :-)

-- 
jan grant, ILRT, University of Bristol. http://www.ilrt.bris.ac.uk/
Tel +44(0)117 9287088 Fax +44 (0)117 9287112 http://ioctl.org/jan/
and Nostradamus never dreamed of the Church of the Accellerated Worm


More information about the freebsd-questions mailing list