[NTLUG:Discuss] searching text in all files for match
Steve Baker
sjbaker1 at airmail.net
Wed Jun 5 01:41:24 CDT 2002
Fred James wrote:
>
> Warning: In UNIX everything is a file. Time aside (and I admit that
> time can be a problem), the only serious issue I have with any of these
> solutions is what happens when they run into "different" files - stuff
> that ain't just text. They can get locked, error out, or if the output
> is redirected to a file, they can even fill all available space. This
> from actual testing this afternoon. That means you will need some means
> of protecting the processes from "different" files, such as searching
> only directories that contain "safe" files. Don't just turn it lose on
> your whole file structure.
> >> Recap:
> >> find . -type f | xargs file | grep -i text | cut -f1 -d: | xargs grep
> >> "search for this"
Well, the 'find . -type f' part will only allow "regular" files through -
so you only have to worry about grepping binary files. The simple way
to fix *that* is to pipe the file through 'strings' before you grep it.
'strings' is a filter that only passes ASCII strings of longer than
a certain limit.
That won't stop it finding matches in binary files - but it will stop
the results of a spurious 'grep' from sending bizarre escape sequences
to your xterm.
Still - it might be better to give 'find' a list of valid file extensions
to match.
----------------------------- Steve Baker -------------------------------
Mail : <sjbaker1 at airmail.net> WorkMail: <sjbaker at link.com>
URLs : http://www.sjbaker.org
http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net
http://prettypoly.sf.net http://freeglut.sf.net
http://toobular.sf.net http://lodestone.sf.net
More information about the Discuss
mailing list