Need unix command file HELP!

Chris Torek chris at umcp-cs.UUCP
Sun Feb 2 01:51:25 AEST 1986


In article <245 at aero.ARPA> sutton at aero.UUCP (Stew Sutton) writes:

> We are looking for a utility that can, when given a arbitrary string,
> locate all occurences of that string anywhere on the system.

First:  Since you want to check all files on the machine, you should
immediately think `find'---likewise for `all files within particular
directory trees', which is of course the general case.

Second:  Since you want to match strings within the files, you
should immediately think `grep' (or variants).

> The utility [should] return all the files (and their pathnames
> from the root) to the screen.

If you want matching lines printed, that is easy; if you want the
entire contents of the file printed, that is also easy.  But I will
assume that the names alone are sufficient.

> Of course if the protections on the file indicate that the file
> cannot be read, the program should ignore that file and keep
> on going.

Also easy: just discard error messages from grep.

So, thinking `find' and `grep' (but I will use egrep since it is
usually faster), you consult the man entries, and . . . :

	$ find / -exec egrep "pattern" {} \; -print 2> /dev/null

This is a bit more difficult in the C shell, as it is unable to
redirect stderr without also redirecting stdout.  However, if you
already know where stdout is going, it can be done:

	% (find / -exec egrep "pattern" {} \; -print > /dev/tty) >& /dev/null

(My own solution to the C shell's redirection quirks is to run `sh'
before doing anything complex.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix mailing list