Need unix command file HELP!

mo at wgivax.UUCP mo at wgivax.UUCP
Sat Feb 15 22:15:49 AEST 1986


>From jsdy at hadron.UUCP (Joseph S. D. Yao) Sun Feb  6 01:28:16 206
>Summary: grep won't always print file name!
>
>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 \;

WRONG!  fgrep -l WILL print the file name, and WILL NOT print the string
        it will look for ONLY the first occurrence in a file, speeding
		things up, AND fgrep is faster than grep

>**OR** (quicker) :
>
>find / -type d -a -exec ksh findstr "this-is-the-string" {} \;

(-: GREAT,  NOW HOW DO I FIND THE KORN SHELL ? :-)

>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

this is admittedly "safer", since it skips non-text files, but look at all
those sub-processes you're starting up for every used inode on the system!

haven't we heard enough about this, YET?



More information about the Comp.unix.wizards mailing list