(parent-dir "/home/sk/parent/ab.el")




(parent-dir "/home/sk/parent")



Any elisp function as parent-dir that gives basepath for both file path & dir path?


file-name-directory does what you request.

However, (file-name-directory "/home/sk/parent/") (with a trailing slash) returns that same directory, not its parent.

This does what I'm guessing you really want:

(file-name-directory (directory-file-name "/home/sk/parent/ab.el")) ; => "/home/sk/parent/"
(file-name-directory (directory-file-name "/home/sk/parent/")) ; => "/home/sk/"
(file-name-directory (directory-file-name "/home/sk/parent")) ; => "/home/sk/"
  • @licorna: I don't understand your question. Did you try evaluating (file-name-directory (directory-file-name "/home/sk/parent"))? directory-file-name removes a final slash, but otherwise it does nothing (on most OS's). And in this case there is no final slash to remove. – Drew Oct 24 '16 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.