Ag command with multiple sources/directory

Thank you for nice article:

But still I have problem with ag getting files from multiple directories. I need Ag to get
directories from both ~/project/ ~/libs/include directories at once and feed FZF

Like at this topic:

only difference is it uses find:
find ~/project/ ~/libs/include -type f

Is it possible with ag?

yes, use -exec

find ~/project/ ~/libs/include -type f -exec command-you-wish-to-run arg1 argN {} +
find ~/project/ ~/libs/include -type f -exec ag "pattern" {} +

Or use xargs:

find ~/project/ ~/libs/include -type f -print0 | xargs -I {} -0 ag "pattern" "{}"

find options

  • -type f - Only search for files
  • -print0 - Display the full file name on the standard output, followed by a null character instead of the newline character that -print uses. This is a failsafe option and useful for xargs -I {} and -0 options.

xargs options

  • {} ag will work on each matched file found by the find
  • -I {} Define filename i.e. variable name for each input file that ag will use.
  • -0 Deal with special file names such as files with white space or weird characters. This is a failsafe option.


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki