3

I want to find every file in from the current dir and downwards whose filename starts with foo using ag(the silver searcher). I have tried:

ag -g '^foo' 
ag -g '\^foo' 
ag -g '\Afoo' 

no luck.

But it should work as ag implements PCRE syntax, right? What am I missing here?

ninrod
  • 379
  • 2
  • 13
  • 1
    Are you sure `echo foo*` isn't enough? – ilkkachu Aug 11 '16 at 21:04
  • 1
    Also, possible `find ./ -type f -name "foo*" -print` – Thomas N Aug 11 '16 at 21:18
  • @ilkkachu, fixed question to include deep dirs. – ninrod Aug 11 '16 at 21:53
  • @ThomasN, I specifically chose `ag` because it ignores patterns inside .gitignore and such, and provides me a way to match strings inside the files. With `ag` I could, for example, use -G and restrict my **grep like search** to the files that match the `^foo` pattern. – ninrod Aug 11 '16 at 21:54

2 Answers2

3

If you do not specify a directory, and therefore search in the current directory, ag seems to prepend './' before each path.

thinkbox ~% mkdir foo
thinkbox ~% cd foo
thinkbox ~/foo% touch bar
thinkbox ~/foo% ag -g '^bar' # not found            
thinkbox 1? ~/foo% ag -g '^\./bar' # found
bar
Harm
  • 96
  • 3
  • Indeed; this looks correct whereas the other answer says ag matches against the full path, which doesn't sound quite right. – dhag Feb 02 '18 at 15:02
1

turns out that ag matches against the full path name of the file. So we have to change the regex in the lines of:

ag -g '/foo[^/]*$'

credits to bmalehorn

ninrod
  • 379
  • 2
  • 13