Need unix command file HELP!

Joseph S. D. Yao jsdy at hadron.UUCP
Tue Feb 11 15:18:39 AEST 1986


All the folk who are responding that the way to get the file names
of files containing a particular string are kind of forgetting that
the grep family does  n o t  automatically print out file names.
This:

>find / -exec fgrep this-is-the-string '{}' \;

will give a file full of lines containing this-is-the-string.  Try:

find / -exec grep this-is-the-string '{}' /dev/null \;

**OR** (quicker) :

find / -type d -a -exec ksh findstr "this-is-the-string" {} \;

findstr:
#!/bin/ksh
# or /bin/sh

str="$1"
dir="$2"
file=""
text=""

if [ ! -d "$dir" ]; then exit 1; fi

cd "$dir"

for file in *; do
	if [ ! -f "$file" ]; then continue; fi
	text=`file "$file" | grep text`
	if [ "" = "$text" ]; then continue; fi
	# if you want the complete text:
	# grep "$str" "$dir/$file" /dev/null
	# otherwise
	text=`grep "str" "$file" | line`
	if [ "" != "$text" ]; then
		echo "$dir/$file"
	fi
done
exit 0
-- 

	Joe Yao		hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}



More information about the Comp.unix.wizards mailing list