Ack is Better Than Grep

Ack is Better Than Grep

Jan 12

  • Created: Jan 12, 2012 1:33 PM

Ack is Better Than Grep

In my last blog posting I wrote about some basic features of grep. If you already use grep a lot, it was probably a pretty boring post. One problem I used to have is when running grep, I’d get a lot of unwanted files that would match. For example I might try searching for the string ‘foobar’ to find where that appears in some code but there’s a DB dump in the directory too and the .sql file has the word ‘foobar’ in it somewhere. And when it does, it will be on one line that has 300 characters on it which fills my screen with its long line, making things hard to skim through.

There are various ways around this problem, you can use the -l flag with grep to just list the files that match and inspect them manually. You can use --include in your grep to only search for certain file types. You can write a find command to find all the file types you’re looking for and pipe that list to grep. All of these are a cumbersome and require too much typing. I’ve seen some people write stuff in bash to only search certain file types, but there is an even easier solution out there: ack!

ack is better than grep, it has almost all the same features as grep then some more. With ack you can specify which file types you want to search, so if you only want to search in php files, you’d run ack --php foobar and it would skip over all file types like .sql, .css, etc. If you want to search all known file types just run ack foobar. It will only search file types it knows but ignores directories named .svn, other VCS directories/files, backup files like foo~ and #foo#, binary files, etc. To search all file types including ones it doesn’t know, you’ll want to use the -a flag but it will still ignore VCS files/directories, to include those use -u.

I use fgrep a lot and was initially disappointed when I found ack had no -F to search for fixed strings, but its just called -Q in ack.

To get a list of all the types ack knows about, run ack --help=types. If you find that list too limiting, the man page provides details on how to add your own types.

Posted in: Linux