Search code examples
ag

How can I use the files found by one ag search as the domain for a second ag search?


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?


Solution

  • 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