4

Suppose I do ag -l foo. That gets me a list of files.

How can I use ag a second time to search within just those files?

joachim
  • 28,554
  • 13
  • 41
  • 44

1 Answers1

7

Assuming you're in the bash shell, you do this:

ag whatever $(ag -l foo)

So to find all the files that match both cat and dog:

ag cat $(ag -l dog)

You could also use xargs:

ag -l dog | xargs ag cat

If you used ack, another greplike tool, you could use the -x option to read the list of input files from stdin:

ack -l dog | ack -x cat
Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • 1
    Note, that `ag -l dog | xargs ag cat` might (partially) break if `ag -l dog` returns paths that contain spaces. To address this, use `-0` on both `ag` and `xargs`, i.e. `ag -l -0 dog | xargs -0 ag cat`. – Michael May 24 '23 at 12:05