29

I am using Silver Searcher to find information in my Calibre library which, by default uses long directory and filenames that are a bit redundant. Example search:

chris@ODYSSEUS:~/db/ebooks/paper-art$ ag --markdown angel

Christophe Boudias (Editor)/Origami Bogota 2014 (Paginas de Origami) (2)/Origami Bogota 2014    (Paginas de Origami) - Christophe Boudias (Editor).md
8:* [16] Angel (???)
9:* [22] Christmas Angel (Uniya Filonova)

Juan Fernando Aguilera (Editor)/Origami Bogota 2013 (Paginas de Origami) (1)/Origami Bogota 2013 (Paginas de Origami) - Juan Fernando Aguilera (Editor).md
29:* [96] Inspired Origami Angel (K. Dianne Stephens)
31:* [100] Angel for Eric Joisel (Kay Kraschewski)

I would like to return just the filename where the whole path is shown in the example. How can I do that?

Chris
  • 871
  • 1
  • 10
  • 19

1 Answers1

45

The l (lowecase L) flag will return the files-with-matches instead of the lines matched.

e.g.

$ ag -l "angel"

you can pipe into sed to remove anything up to and including the final / which leaves the filename.

ag -l angel | sed 's=.*/=='

zsoobhan
  • 1,484
  • 15
  • 18
  • That still returns the whole path along with the filename...I'm trying to return ONLY the filename, no path! – Chris Jan 14 '15 at 02:01
  • Apologies, I missed that. Piping to SED should do it (see edit). – zsoobhan Jan 16 '15 at 01:03
  • 5
    we could use `basename` to get that: `ag -l angel | xargs basename` – ryan Apr 11 '17 at 03:18
  • 2
    `xargs -n 1 basename` (_`-n max-args, --max-args=max-args`: Use at most max-args arguments per command line_). If not you might get `basename: extra operand` error. – Pablo Bianchi Jul 15 '17 at 00:45