14

How can I search all files (including binary) in the current directory with ack v2?

I want to do the same as doing ack 'foo' **, but just with a flag, so it works if I want to search all files in a specific directory without appending **.


Difference between ack 'foo' and ack 'foo' **:

$ mkdir test && cd test
$ printf '\x00\x01foo1\x00' > test1
$ printf 'foo2' > test2
$ ack 'foo'
test2
1:foo2
$ ack 'foo' **
test1
1:foo1

test2
1:foo2
Tyilo
  • 5,981

2 Answers2

15

When you pass no file name to ack, it operates on a built-in default set of files that excludes unrecognized binary files. If you do pass file names, it searches in all the files you pass. This explains the difference between ack foo and ack foo **.

ack 1

In ack 1.x, use the flag -a to tell ack not to skip files whose type it doesn't recognize.

You can make an alias if you want that flag more often than not: add the line alias ack='ack -a' in your ~/.bashrc or ~/.zshrc. If you set up this alias and want to call ack without the option, call \ack (the backslash disables alias expansion). Scripts are not affected by the alias, only interactive shells.

Alternatively, you can add the -a option to your ~/.ackrc.

Matches in binary files will be listed as “Binary file test1 matches”. There is no way to disable this behavior without modifying Ack itself (at least adding a plugin).

ack 2

In ack 2.0, the -a option has been removed. What you can do instead is to define a type that matches all files, and use the -k option to make ack include all files of that type. Note that file types are processed in an unpredictable order, to this will cause a random set of binary types to be processed as ordinary instead of as the usual type.

ack --type-set='all:match:.*' -k foo

This way the matches are printed even for files that would otherwise look binary. As above, you can add these options to an alias or to your .ackrc.

7

By default, Ack searches for a pattern in all non-binary files below the current directory.

I believe older Ack versions would search through binary files when you passed them the --binary flag. This flag has been removed though. You may be able to use grep instead.

grep -ar 'foo' .