3

While I can build a pathname e.g.

(make-pathname :directory '(:RELATIVE "dir" "subdir" "subsubdir"))

how do I get back subsubdir from a pathname like this (assuming it is a directory)? I need to extract the last dir from a pathname, just as this Unix command does:

$ basename /usr/local/share/
share
PeeHaa
  • 71,436
  • 58
  • 190
  • 262

1 Answers1

8

See the Common Lisp Hyperspec, the Filenames Dictionary

(first (last (pathname-directory some-pathname)))
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • This only handles the case where the path ends with a slash. If you have a path `a/b/c`, the code above will return `b` while basename returns `c`. – Kevin Chen May 05 '17 at 01:45
  • @KevinChen: he was asking using Lisp pathnames. The Unix basename command was just an example. – Rainer Joswig May 05 '17 at 06:27
  • 1
    @KevinChen If the path does not end with a slash, use `(file-namestring "/a/b/c")` instead. – Flux Mar 02 '22 at 07:36