I’ve been using the Unix
grep utility for over 20 years but today I learned something new. Mikko Ohtamaa over at Open Source Hacker has a nice post on Power searching using UNIX grep. The post explores some of the enhancements to the GNU version of
grep. These days, even Mac OS X comes with GNU
grep so it’s a worthwhile read.
One of the things you often to do with
grep is to search recursively into subdirectories but the older versions had no way of filtering on file type. Thus, you could say something like
grep -R foo ~/projects
to search through the
projects subtree but there was no way to specify which files you wanted to consider. If there were binary files under the
projects subdirectory you would search them too. Fortunately, GNU
grep has a way around this. If we want to search just C files we would specify something like
grep -R --include="*.c" foo ~/projects
It’s hard to believe that I didn’t know about this after all the time I’ve been using
grep. My only defense is that I learned
grep early on and had little reason to revisit the documentation. In any event, it’s comforting to know that my education is ongoing.