62

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.

Pablo A
  • 2,307
  • 1
  • 22
  • 34
Dave
  • 2,348
  • 21
  • 54
  • 84
  • 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 08 '17 at 21:22

2 Answers2

80

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

don_crissti
  • 79,330
  • 30
  • 216
  • 245
5

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.

Sienna
  • 265
  • 2
  • 7