grep replacement for development work


Radio Telescope in the desertBrad Choate mentioned Andy Lester's ack tool a few days ago.

It's, as Brad writes, basically a replacement for the "grep" search tool.

One of the big features (that I don't care all that much for) is that the output tries to be more helpful than the usual grep output. My habit is to grep for something and pipe it to "less" and then do more searches with that tool, so ack isn't too helpful there.

However, I do use ack all the time because of another major feature: it automatically ignores files I don't care about (files in .svn directories, binary files etc)! Brilliant. No more long command lines (or extra scripts) to filter those out.

It does it by file extension (for speed) and it could use a few more added (Andy will take patches, I am sure) for .css files for example.


I'm sure ack works just perfectly for what Andy Lester needs to search for in his particular environment, but I don't think I could depend on a system installed tool with such specific inflexible rules, so I make my own that I can adjust to my current needs.

If your main goal is to ignore certain file extensions and subdirectories, here's a very simple but easily extendable script that does the basics.

# grep recursively from current directory skipping files and
# directories that are probably not interesting.
find . -type f \
-not -name '*~' \
-not -name '.#*' \
-not -name '*#' \
-not -name '*.gif' \
-not -name '*.jpg' \
-not -name '*.zip' \
-not -name '*.gz' \
-not -name '*.obj' \
-print0 \
-o -name '.svn' -prune \
-o -name 'CVS' -prune \
| xargs --null egrep "$@"

It accepts all of the familiar egrep options and regexp syntax, so you can do things like:

$ mygrep -i -l -- '->method_name'

One of the big features is that it uses Perl's regexes, so you don't have to wonder about if egrep handles things like \b or not.

The main reason I put it out there is that there are just so many of those "ignore everything" shell scripts out there that I figured there should be something a bit more standard.

It also lets you include only certain types of files, so you can say

ack --perl username

and ignore all the javascript programs that contain "username" as well.

Seriously, try it out. Or not. I suspect you won't find it inflexible.

Oh, and in a case of great minds thinking alike: --css is supported as of 1.25_01, based on a patch from someone else.

what about GNU id-utils or GLOBAL?

Note that ack now has its own homepage, off the CPAN, at

Leave a comment

About this Entry

This page contains a single entry by Ask Bjørn Hansen published on August 15, 2006 7:24 PM.

Find recent content on the main index or look in the archives to find all content.


OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.33-en
/* bf */