This article explains absolute paths and how they differ from relative paths, getting them, and how symbolic links are handled.
A path is the location of a file in a file system. It’s the directions to the file in the folder it is located.
A path consists of a string of characters. Some represent directory names, and a separator character separates the directory names from the file name and extension.
Consider the below path:
It consists of:
- Forward slashes (/) to separate directories from their subdirectories
- Directory Names – the text between the forward slashes
- The file name – in this case, file.txt
Relative paths are paths that are defined in relation to your current position in the file system.
You can find your current position using the pwd command:
Here’s how to use the pwd command.
Relative paths begin without a forward slash, or a . or ...
- Paths beginning without a / or with a . start in the current directory
- Paths beginning with .. start in the directory above the current directory (the parent of the current directory)
If you are in the directory /home/user:
…and it contains a file called test.txt, you need only type:
…to view the contents of the file, as you are in the same directory as that file, and can access it using its relative path.
In the above example, the cd command is used to change the directory, and the cat command reads the contents of the file to the screen.
Absolute paths can be used from anywhere on the filesystem. They represent the full path of the file from the root (the very top) of the file system hierarchy. They are absolute because it’s the absolute location of the file on the file system. It is not relative to where you are or where you might be; the path will work when called from any location.
Consider again that we are in the directory /home/user:
…and there is a file called another.txt located at /var/temp. Attempting to open it by running:
…will fail because that file doesn’t exist at /home/user. We can, however, access it at its absolute path:
As it provides the full location of the file and does not rely on the relative path we are currently situated.
Paths and Hard / Soft Links (symlinks)
Soft links (symlinks) are just files that point to another file. The absolute path is still the path to the symlink. If you want the path to the linked file itself, you will need to use the readlink command to find the absolute path of the linked file:
readlink -f /path/to/symlink
Find out more about readlink here.
Hard links are also absolute paths – the data exists at multiple absolute paths but in only one location on the physical disk.