elisp: Relative File Names
24.8.2 Absolute and Relative File Names
---------------------------------------
All the directories in the file system form a tree starting at the root
directory. A file name can specify all the directory names starting
from the root of the tree; then it is called an “absolute” file name.
Or it can specify the position of the file in the tree relative to a
default directory; then it is called a “relative” file name. On Unix
and GNU/Linux, an absolute file name starts with a ‘/’ or a ‘~’ (
abbreviate-file-name), and a relative one does not. On MS-DOS and
MS-Windows, an absolute file name starts with a slash or a backslash, or
with a drive specification ‘X:/’, where X is the “drive letter”.
-- Function: file-name-absolute-p filename
This function returns ‘t’ if file FILENAME is an absolute file
name, ‘nil’ otherwise.
(file-name-absolute-p "~rms/foo")
⇒ t
(file-name-absolute-p "rms/foo")
⇒ nil
(file-name-absolute-p "/user/rms/foo")
⇒ t
Given a possibly relative file name, you can convert it to an
absolute name using ‘expand-file-name’ (File Name Expansion).
This function converts absolute file names to relative names:
-- Function: file-relative-name filename &optional directory
This function tries to return a relative name that is equivalent to
FILENAME, assuming the result will be interpreted relative to
DIRECTORY (an absolute directory name or directory file name). If
DIRECTORY is omitted or ‘nil’, it defaults to the current buffer’s
default directory.
On some operating systems, an absolute file name begins with a
device name. On such systems, FILENAME has no relative equivalent
based on DIRECTORY if they start with two different device names.
In this case, ‘file-relative-name’ returns FILENAME in absolute
form.
(file-relative-name "/foo/bar" "/foo/")
⇒ "bar"
(file-relative-name "/foo/bar" "/hack/")
⇒ "../foo/bar"