There is generally no such thing as the absolute path
to a file (this statement means that there may be more than one in general, hence the use of the definite article the is not appropriate). An absolute path
is any path that start from the root "/" and designates a file without ambiguity independently of the working directory.(see for example wikipedia).
A relative path
is a path that is to be interpreted starting from another directory. It may be the working directory if it is a relative path
being manipulated by an application
(though not necessarily). When it is in a symbolic link in a directory, it is generally intended to be relative to that directory (though the user may have other uses in mind).
Hence an absolute path is just a path relative to the root directory.
A path (absolute or relative) may or may not contain symbolic links. If it does not, it is also somewhat impervious to changes in the linking structure, but this is not necessarily required or even desirable. Some people call canonical path
( or canonical file name
or resolved path
) an absolute path
in which all symbolic links have been resolved, i.e. have been replaced by a path to whetever they link to. The commands realpath
and readlink
both look for a canonical path, but only realpath
has an option for getting an absolute path without bothering to resolve symbolic links (along with several other options to get various kind of paths, absolute or relative to some directory).
This calls for several remarks:
- symbolic links can only be resolved if whatever they are supposed to
link to is already created, which is obviously not always the case. The commands
realpath
andreadlink
have options to account for that. - a directory on a path can later become a symbolic link, which means that the path is no longer
canonical
. Hence the concept is time (or environment) dependent. - even in the ideal case, when all symbolic links can be resolved,
there may still be more than one
canonical path
to a file, for two reasons:- the partition containing the file may have been mounted simultaneously (
ro
) on several mount points. - there may be hard links to the file, meaning essentially the the file exists in several different directories.
- the partition containing the file may have been mounted simultaneously (
Hence, even with the much more restrictive definition of canonical path
, there may be several canonical paths to a file. This also means that the qualifier canonical
is somewhat inadequate since it usually implies a notion of uniqueness.
This expands a brief discussion of the topic in an answer to another similar question at Bash: retrieve absolute path given relative
My conclusion is that realpath
is better designed and much more flexible than readlink
.
The only use of readlink
that is not covered by realpath
is the call without option returning the value of a symbolic link.