Function: file-name-parent-directory
file-name-parent-directory is a byte-compiled function defined in
files.el.gz.
Signature
(file-name-parent-directory FILENAME)
Documentation
Return the directory name of the parent directory of FILENAME.
If FILENAME is at the root of the filesystem, return nil.
If FILENAME is relative, it is interpreted to be relative
to default-directory, and the result will also be relative.
Other relevant functions are documented in the file-name group.
Probably introduced at or before Emacs version 29.1.
Shortdoc
;; file-name
(file-name-parent-directory "/foo/bar")
=> "/foo/"
(file-name-parent-directory "/foo/")
=> "/"
(file-name-parent-directory "foo/bar")
=> "foo/"
(file-name-parent-directory "foo")
=> "./"
Source Code
;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun file-name-parent-directory (filename)
"Return the directory name of the parent directory of FILENAME.
If FILENAME is at the root of the filesystem, return nil.
If FILENAME is relative, it is interpreted to be relative
to `default-directory', and the result will also be relative."
(let* ((expanded-filename (expand-file-name filename))
(parent (file-name-directory (directory-file-name expanded-filename))))
(cond
;; filename is at top-level, therefore no parent
((or (null parent)
;; `equal' is enough, we don't need to resolve symlinks here
;; with `file-equal-p', also for performance
(equal parent expanded-filename))
nil)
;; filename is relative, return relative parent
((not (file-name-absolute-p filename))
(file-relative-name parent))
(t
parent))))