1

Let's say I have two files in a directory:

.test
test.txt

How can I limit my ag search to only files that begin with a .?

I have tried a number of patterns, but none worked.

a-b-r-o-w-n
  • 515
  • 2
  • 17
  • Well I stumbled upon a way to do this using zsh's expansion, but it only works by listing the files in the current directory (`ag foo .*` where `.*` resolves to `.test`). This was ok for my current need, but not satisfactory as a robust solution. – a-b-r-o-w-n Apr 05 '17 at 18:43

1 Answers1

2

How can I limit my ag search to only files that begin with a .?

Like this:

ag -u -G ^\..*$ PATTERN

In your example, the file .test is hidden, so the option -u comes in handy. This option is probably what you missed in your search.

Let me explain the regular expression ^\..*$:

  • ^ means: the beginning of the string
  • \. means: a dot character
  • .* means: any character (.) any number of times (*)
  • $ is optional; it marks the end of the string to match

Note: I opened a bug tracker for ag, because I see the search is not limited to files whose names start with . as it should be for this regular expression.


More generally, to search PATTERN in files whose names match REGEX, one can use:

ag -G REGEX PATTERN

(same as ag --file-search-regex REGEX PATTERN)

Check the manual for more information on ag.

Bludzee
  • 2,733
  • 5
  • 38
  • 46
  • Ah, so this is currently a bug with ag! I was trying many variations of that pattern without luck. Care to link to the issue so I can follow? – a-b-r-o-w-n Apr 07 '17 at 16:09
  • I updated the bug tracker to provide debug info (using ag option `-D`). – Bludzee Apr 10 '17 at 08:09