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
  • Why do you want to use ack for this instead of grep? – Andy Lester Jul 27 '13 at 17:55
  • 1
    @AndyLester I just thought that ack is always better than grep. I was wrong :). – Tyilo Jul 27 '13 at 18:07
6

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' .
  • It has nothing to do with symlinks. It has to do with binary files! – Tyilo Jul 23 '13 at 0:44
  • @Tyilo: I have modified my answer. – user26112 Jul 23 '13 at 0:57
13

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.

  • 1
    That's pretty sneaky, @Gilles. – Andy Lester Jul 27 '13 at 17:55
  • @AndyLester, why was the -a option removed? The "This is because" in the return doesn't actually explain why I can't search unrecognized file types anymore. – Yann Dìnendal May 22 '14 at 16:02
  • Because 1.x's -a behavior, to search all unrecognized text files, is now the default in 2.0. – Andy Lester May 22 '14 at 17:50
  • 2
    Weird, I have a minified js file that even that special match-all-types type-set can't find... – Izkata Jan 28 at 19:14
  • I'm here because of that: a text inside a minified js file, that is not parsed by ack. I thought the answer would be like "add -a"... I'm dissapointed. – Luciano Fantuzzi Feb 14 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.