A good definition of a canonical path is given in the documentation of readlink
in GNU Coreutils. It is specified that 'Canonicalize mode' returns an equivalent path that doesn't have any of these things:
- hard links to self (.) and parent (..) directories
- repeated separators (/)
- symbolic links
The string length is irrelevant, as is demonstrated in the following example.
You can experiment with readlink -f
(canonicalize mode) or its preferred equivalent command realpath
to see the difference between an 'absolute path' and a 'canonical absolute path' for some programs on your system if you are running linux or are using GNU Coreutils.
I can get the path of 'java' on my system using which
$ which java/usr/bin/java
This path, however, is actually a symbolic link to another symbolic link. This symbolic link chain can be displayed using namei
.
$ namei $(which java)f: /usr/bin/java d / d usr d bin l java -> /etc/alternatives/java d / d etc d alternatives l java -> /usr/lib/jvm/java-17-openjdk-amd64/bin/java d / d usr d lib d jvm d java-17-openjdk-amd64 d bin - java
The canonical path can be found using the previously mentioned realpath
command.
$ realpath $(which java)/usr/lib/jvm/java-17-openjdk-amd64/bin/java