ack: Exclude specific directories from search via regex
Asked Answered
I

4

27

How do I ignore specific directories via RegEx with ack?

I can use the --ignore-dir option, but this does not let me specify a RegEx. I want to be able to ignore any directory, which has the words test or tests or more complicated patterns in its name.

I also tried a negative lookbehind via

ack -G '(?<!test)' pattern

but this does not work. It does not exclude the test directories.

Ivar answered 29/12, 2011 at 15:35 Comment(0)
A
22

Use the undocumented option "--invert-file-match" (ack version on my system: 1.96):

$ ack pattern -G 'test|tests' --invert-file-match

Well, it is sort of documented:

$ ack --help|grep invert
-v, --invert-match    Invert match: select non-matching lines
--invert-file-match   Print/search handle files that do not match -g/-G.

It is not documented in its perldoc.

Autoicous answered 29/12, 2011 at 16:31 Comment(1)
Awesome -- thanks! Nothing about this in the man page I'm looking at either, but it works.Transpadane
M
11

With ack2, it seems you can't use holygeek's solution.

Here's how I'd do it using -v and -x:

ack -v -g 'test' | ack -x pattern

More generally, 'test' can be a regex for dirs to exclude

Mut answered 20/9, 2013 at 13:6 Comment(0)
B
6

In recent versions of ack, you can use regular expressions with --ignore-dir.

From this thread:

Yes, in 2.15_01 we added:

  • ack now supports --ignore-dir=match:.... Thanks, Ailin Nemui! (GitHub ticket #42)

GitHub link: https://github.com/petdance/ack2/issues/42

Sadly, I think this only supports matching the base directory name, not the full path.

For instance, you cannot match a path like ".*/docs/generated/.*" with it.

For that, see the GitHub issue 291.

update for 2020: it looks like this is now moved to ack3 (github issue).

Bradleybradly answered 15/3, 2017 at 18:19 Comment(0)
Q
5

In the interest of folks using a pre-1.96 version of ack (like me), you can use regex look around to do this. Here's an example:

ack --java 'text-pattern' -G '^((?!(test|target)).)*$'

This will search the text-pattern in all Java files recursively (from .) that DO NOT have the words test or target in their path.

Quadrifid answered 20/8, 2012 at 21:58 Comment(1)
What does this construction '^((?!(test|target)).)*$' mean?Pulitzer

© 2022 - 2024 — McMap. All rights reserved.