Absolute and relative paths
Video 102 of 140 in Virtualization Foundations, in the unit “The shell and the filesystem tree”. 2:42, with English and Hindi audio.
A file name on its own is often not enough. The shell needs a path, which tells it where the file is. There are two kinds of path: an absolute path starts at the top of the tree, and a relative path starts from the folder you are in. Both are useful, and both can fail in different ways. This article explains the two kinds, shows how to move around with cd and pwd, and explains why a script that works by hand can fail when a scheduled job runs it.
What you will learn
- An absolute path starts with a slash at the top of the tree, so it means the same place from anywhere.
- A relative path starts from the folder you are in, so the same name can point somewhere else.
- Two dots mean the folder above, and a single dot means this folder.
- The pwd command prints the folder you are in, and cd moves you.
- Scheduled jobs often start in a folder you did not expect, so scripts should use full paths.
Two ways to name a place in the tree
A path names a place in the tree. An absolute path starts with a slash, which is the top of the tree, so it means the same place from anywhere. A relative path starts from the folder you are standing in, so the same short name can mean something different in another folder.
- Absolute path: starts with a slash, and always names the same place.
- Relative path: starts from the current folder, and changes when you move.
- Two dots: the folder above the one you are in. A single dot means this folder.
Moving with cd and checking with pwd
The pwd command prints your current folder, and cd moves you to another one. Start by moving to an absolute path, then move with a relative path that uses two dots.
cd /etccd ../var/logFrom the log folder, two levels up reach the top of the tree. Home is one folder down from there, so a path of two dots, two dots and home moves you to the home folder.
Why a short name fails in a scheduled job
Picture a small backup script that looks for a sales file by a short, relative name. When an engineer runs the script by hand from the home folder, the short name points at the file, and the copy works.
A scheduled job can start the same script from a different folder. The short name then points at nothing, and the copy fails. An absolute path, starting with a slash, names the file in full, so the script works from any folder.
Note: When a script runs from a scheduler, do not rely on the folder it starts in. Write the full path of every file the script needs.
Try it yourself
You need a Linux terminal. Move around the tree with absolute and relative paths, and check your position after each move.
Move to /etc with an absolute path, which means the same place from anywhere.
cd /etcPrint the folder you are in now.
pwdWhat you should see: The shell prints /etc.
From etc, use a relative path with two dots. It goes up one level, then down into var and its log folder.
cd ../var/logPrint the folder again, to check the move.
pwdWhat you should see: The shell prints /var/log.
Go up two levels from the log folder, then down into home.
cd ../../homePrint the folder once more.
pwdWhat you should see: The shell prints /home.
Terms
- Absolute path
- A path that starts at the top of the tree with a slash, and names the same place from any folder.
- Relative path
- A path that starts from the folder you are in, so its meaning changes when you move.
Check yourself
Why does a backup script that works by hand fail in a scheduled job?
A relative name is read from the folder the script starts in. The scheduled job may start in another folder, where the name points at nothing.
What do two dots in a path mean?
The folder above the one you are in. A single dot means the current folder.
Which command shows the folder you are in?
pwd. It prints the current folder, so you can check where you are before you act.
Previous: Getting help on the command line. Next: The Linux directory tree.