What is the difference between absolute / canonical / relative paths?
I guess that absolute starts with a /. Can it contain /../?
Is a canonical path different from an absolute path in that it can’t contain /../?
Is foo a relative path?
Googling posix canonical path didn’t turn up much… What are the POSIX references for these terms?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
POSIX defines “absolute pathname” as follows:
A pathname beginning with a single or more than two <slash> characters
and “relative pathname” as follows:
A pathname not beginning with a <slash> character.
That’s all there is to it for relative and absolute paths.
Canonical paths aren’t defined in POSIX, but the term usually refers to comparable paths, i.e. if you take two paths to a file system object, and convert them to canonical form, the result should be identical if and only if the two file system objects are identical. This involves removing “..” as you mention, but it also means resolving symbolic links; so a canonical path could be defined as
A pathname whose components are all real directories or files, excluding “.” and “..”, and whose slashes are not repeated
In POSIX terms, a canonical pathname is effectively a resolved pathname (as long as you accept that canonical pathnames can only be determined for file system objects which exist).
Note that this only works because hard-linked directories aren’t allowed…
So to answer your questions:
- an absolute path can contain
/../; - a canonical path can not contain
/../, nor can it contain/./,//(except arguably in first position), or symbolic links; foois a relative path.
(Pedantically, they are all pathnames, not just paths.)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0