ack = grep for programmers

Pavel Saman
2 min readSep 29, 2023

I’ve recently found ack which seems like an interesting tool that can, in some situations, save a bit of time.

$ sudo zypper in ack

(or whatever your package manager or means of installing new software is)

The man page says:

NAME
ack — grep-like text finder

SYNOPSIS
ack [options] PATTERN [FILE…]
ack -f [options] [DIRECTORY…]

DESCRIPTION
ack is designed as an alternative to grep for programmers.

ack searches the named input FILEs or DIRECTORYs for lines containing a
match to the given PATTERN. By default, ack prints the matching lines.
If no FILE or DIRECTORY is given, the current directory will be
searched.

I don’t often use grep for searching for patterns in files, but if I need it, I can imagine, ack can save a little bit of time.

Let’s say I need to search for cov[A-Z] pattern in all files in src/ directory. I can use grep:

$ grep -Pr 'cov[A-Z]' src/

where -P turns on Perl regexes and -r stands for recursive. The Perl regexes mode is not needed for this example, but ack operates in this mode, so to make the grep examples closer to the ack examples, I decided to use it.

This is a simple example, so the benefit of ack won’t stand out that much. But even on this example, I need to remember, or search in man pages, some of the options grep offers.

With ack, the same task would be:

$ ack 'cov[A-Z]' src/

Now imagine I want to search only in *.ts files:

$ grep -Pr --include="*.ts" 'cov[A-Z]' src/

Not that bad, but already some typing. With ack, it gets shorter:

$ ack --ts 'cov[A-Z]' src/

Ack is more tuned to developers and their project directories. That’s what makes it a bit more easier to use in such cases. For example, ack automatically skips searching some version control directories like .git/.

--ts option in the last example means a type, there are many other types ack supports, run ack --help-types and see them yourself. One type can encapsulate many different files, for example bash type includes all these:

.sh .bash .csh .tcsh .ksh .zsh .fish; First line matches /^#!.*\b(?:ba|t?c|k|z|fi)?sh\b/

You can add your own file types in case you’re missing something.

If you need to use a certain ack option often, you might specify it in $HOME/.ackrc and ack will automatically use it every time it’s invoked.

All in all, it’s an interesting tool. It’s not like grep would be forgotten from now on because its purpose does not completely overlap with that of ack, but ack can deliver. Give it a try :)

--

--