find myDir -name '*.html' -print0 | xargs -0 grep substr /dev/null

Notes:

Find recurses through the directory tree myDir... use "." if you want 
to search from the current directory.

-print0 delimits file names with a null character, so file names with 
embedded white space (spaces, tabs, newlines) in their names will still 
work.  This can be important when traversing a filesystem that might 
contain Windows created files (such as those hosted by a samba server).

xargs reads arguments from it's stdin and runs as many invocations of 
grep as required.  The -0 argument tells it that the file names are 
null terminated (corresponding to -print0).

Putting the /dev/null on is important, because you don't know how many 
files find will find.  If it happens to find one file, or some number 
of files which modulo divided by the number of arguments allowed, it is 
possible for grep to be called with a single file name as an argument.  
In this case, grep will assume you know what file you gave it (since 
you only gave it one file name) and will not print the name of the file 
in which the matching strings were found.  By giving grep an argument 
of /dev/null, you are guaranteeing that it always has at least two 
filename arguments.  Obviously, the searched for string will not be 
found in /dev/null.

This method is the most portable way of doing your recursive grep and 
works on any unix that supports -print0 and xargs -0.  If you find 
yourself on a unix where the find command doesn't support -print0 and 
xargs doesn't support -0 (or you know for a fact that you don't have 
any file names with spaces in them), you can use -print and xargs 
without the -0 option.

        -jan-
-- 
Jan L. Peterson
Semi-Unemployed "Computer Facilitator"
http://www.peterson.ath.cx/~jlp/resume.html



____________________
BYU Unix Users Group 
http://uug.byu.edu/ 
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list

Reply via email to