grep

rbottin at atl.calstate.edu rbottin at atl.calstate.edu
Sat Nov 3 13:59:55 AEST 1990


tiefel at sunshine.Kodak.com asked for a way to searach a structure
for files that have a string. Here are some probable solutions:

find some_directory -type f -exec grep string '{}' \; -print

This is hard work and puts file names after thelines that match.

for d in *;do grep string  $d/*; done
is bourne/Korn shell dependent and may be faster if ONLY one level
needs to be searched (and has no directories).

ls -Rf|while read f;do grep string $f && echo $f; done

is faster than the 'find'....and there is a similar 'awk'ish solution

ls...|awk '{system("grep string " $0)}'
which fails to indicate which file the line occurs, but is kind of neat
otherwise.

The ultimate helper would be a little script called "hunter" perhaps
: Usage:  hunter string directory
if [ $# -ne 2 ]; then echo Usage: hunter string directory; exit 1; fi
for d in $2/*
do
	if [ -d $d ]
	then hunter $1 $d
	elif grep $1 $d >/dev/null
	then echo $d; grep $1 $d   # inefficient but avoids tmpfiles
	fi
done
: disclaimer - this is a quick hack and needs testing.

Dick Botting CalState San Bernardino
rbottin at atl.calstate.edu



More information about the Comp.unix.questions mailing list