2

I like to search for directories only, and the option ag --list-file-types is not helpful with that. The reason to stick to ag is the possibility to pick a file with --path-to-ignore containing patterns to ignore.

An equivalent command would be fd --type d.

Kevin Hernandez
  • 1,270
  • 2
  • 19
  • 41
F. Röder
  • 21
  • 2

2 Answers2

2

Use the -G argument, and specify a directory name.

For example, if you wanted to search for the word "foobar" in only your Downloads directory:

ag -G "Downloads/" foobar
gregory
  • 10,969
  • 2
  • 30
  • 42
0

You can list of all directories, recursively from your working directory, using ag in a chained command:

ag --null -g ./ | xargs -0 dirname | sort -u
  • --null means each ag output is separated by a \000, which allows special characters to be handled by xargs
  • -g means we only search in filenames, not inside the files themselves
  • ./ running in the current working directory
  • | xargs -0 - pipe all the (relative) file paths from ag into xargs, which uses -0 to accept the --null output we get from ag
  • dirname returns just the directory name of each file
  • sort -u we sort the directories returned and deduplicate them. We will have the same directory ones for every file it contains!

You can then pass the output of that list to grep of something fancier, like [fzf][1].

n1k31t4
  • 2,745
  • 2
  • 24
  • 38