Many times I've run into a situation where I want to grep somethingin every file of a particular type in a certain directory tree recursively. For example, find all the html files (*.html filemask) containing 'substr' in myDir/.

Using grep for this purpose, I can do

grep -l 'substr' myDir/*.html

if all the files are in one directory. Alternatively, I can do

grep -lr 'substr' myDir/

and it will check all files recursively. But I can't effectively combine the two (-r and a filemask).

grep -lr 'substr' myDir/*.html

won't descend into myDir/subdir/, because subdir doesn't match the *.html filemask. And if for some odd reason, I had a subdirectory with a .html extension, all files in that subdirectory (not just the *.html files) will then be grepped. This is because the wildcard expansion is done by the shell.

Currently, I can do this to get the desired results:

grep -lr 'substr' myDir/ | egrep "html$"

But I don't like this because it's extremely inefficient. In mya particular case, the desired filetype makes up maybe 10% of the files in the tree, and most of the files that I'm *not* interested in are fairly large (images, etc.), so I'd like if grep would just ignore them.

I can write my own script to choose the files and then apply grep to them, and I can apply a find/xargs combo, but I was just wondering if anyone knew of some other elegant, 'shweet' way of doing this?

Jacob Fugal


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

Reply via email to