39

Is it possible to ignore multiple directories in Ack, without repeating the flag?

e.g. I know the following works (i.e. setting multiple flags):

ack --ignore-dir=install --ignore-dir=php 'teststring'

I was hoping that I could separate directories with commas, like I can do with the extensions as follows:

ack --ignore-file=ext:css,scss,orig 'teststring'

However, the following comma separated ignore flag doesn't work:

ack --ignore-dir=install,php 'textstring'

Is it possible to use some short-hand equivalent, so I don't have to repeatedly type out the --ignore-dir flag?

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
SparrwHawk
  • 13,581
  • 22
  • 61
  • 91

4 Answers4

56

It's actually similar to how you would specify the include patterns for grep:

ack <term> --ignore-dir={dir_a,dir_b}

However, This format does not work with a single directory. So

ack <term> --ignore-dir={log}

will not work.

Mandar Pathak
  • 839
  • 6
  • 17
  • 15
    Note that the {} syntax causes a bash-expansion - it's not specific to ack. Try this at your command line: `echo this is cool{1,2}` – AgentLiquid Jan 08 '15 at 19:09
  • 9
    It's also important to point out that because brace expansion is a bash-specific feature, you won't be able to take advantage of it in your `.ackrc` file. Instead you will need to list out each `--ignore-dir=` option individually. – wpcarro Aug 26 '16 at 14:22
  • I found that spacing between the dir names matters: `ack --ignore-dir={dir_a,dir_b}` works but `ack --ignore-dir={dir_a, dir_b}` doesn't – kip2 Mar 21 '19 at 16:57
22

Since you're using ack 2, you can put --ignore-dir=install and --ignore-dir=php in a .ackrc file in the root of your project. Then, every ack invocation in that tree will use those flags.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
18

So to ignore single directory use

ack <term> --ignore-dir=dir_a

and to ignore multiple directories use

ack <term> --ignore-dir={dir_a,dir_b}
Kaless1n
  • 313
  • 2
  • 8
0

One approach could be to select those directories to exclude with a regular expression in -G option complemented with the option --invert-file-match. Based in your question, something like the following:

ack -a -G 'install|php' --invert-file-match 'textstring' .
Birei
  • 35,723
  • 2
  • 77
  • 82
  • Interesting, but I don't think this is viable in Ack 2. I get the following message: `Option '-a' is not valid in ack 2 This is because we now have -k/--known-types which makes it only select files of known types, rather than any text file (which is the behavior of ack 1.x). You may have options in a .ackrc, or in the ACKRC_OPTIONS environment variable. Try using the --dump flag` – SparrwHawk Dec 17 '13 at 12:05
  • @Birei's solution only works on ack 1.x. The -a and -G flags have been removed in ack 2 in favor of the -x flag. – Andy Lester Jan 16 '14 at 18:48