69

I'm using ag (The Silver Searcher) version 0.31.0. I can easily look for a string in a bunch of files using:

localhost:workspace davea$ ag 'ftp' .

But what if I only want to scan files with certain extensions? I tried this:

localhost:workspace davea$ ag 'ftp' .java
ERR: Error stat()ing: .java
ERR: Error opening directory .java: No such file or directory

but got the errors you see above.

1
  • Gave that a whirl but got "ERR: Error stat()ing: *.java" and "ERR: Error opening directory *.java: No such file or directory" errors
    – Dave
    Feb 8, 2017 at 21:22

2 Answers 2

90

Per the manual, you could use ag with -G

   -G --file-search-regex PATTERN
          Only search files whose names match PATTERN.

e.g.

ag -G '\.java$' 'ftp' .

Per the same manual

   It is possible to restrict the types of files searched [...]
   For a list of supported types, run ag --list-file-types.

So you could also run

ag --java 'ftp' .

though that would restrict the search to file names ending in .java or .properties

3
  • 2
    oops sorry, deleting. Sep 17, 2018 at 20:10
  • 1
    ` ag -G '\.(hbs|html)$' keyword ` if you are searching multiple file extensions
    – Alan Dong
    May 9, 2019 at 20:40
  • works perfectly with Cygwin on Windows 10 64bit. For example to search for word avg across all .xml files, I used this: ag -G '.xml' avg
    – alphaGeek
    Nov 19, 2020 at 8:07
6

As of ag version 2.2.0, you can run ag --list-file-types, which will list supported, out-of-the-box types. You can run ag "ftp" --java and search against only varying Java files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .