ack: Exclude specific directories from search via regex

二次信任 提交于 2019-12-02 20:22:38

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.

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

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.

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 (still open) GitHub issue 291.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!