Silver Searcher: how to return filename without path
Asked Answered
P

1

30

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?

Patience answered 27/11, 2014 at 0:57 Comment(0)
A
46

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=.*/=='

Ablution answered 6/1, 2015 at 17:2 Comment(4)
That still returns the whole path along with the filename...I'm trying to return ONLY the filename, no path!Patience
Apologies, I missed that. Piping to SED should do it (see edit).Ablution
we could use basename to get that: ag -l angel | xargs basenameMisgovern
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.Leandroleaning

© 2022 - 2024 — McMap. All rights reserved.