absolute and relative paths
When specifying the location of files and folders on a computer system, it is important to know the difference between a relative path and an absolute path, and which is most appropriate for your code.
table of contents
absolute path
An absolute path is the path to a file or directory from the root of the computer.
On Unix-like systems, absolute paths begin with /
(e.g. /Users/username/
).
On Windows systems, absolute paths begin with the drive name (e.g. C:/Users/username/
).
In a Unix shell, you print the absolute path of your working directory with the pwd
command. Additional information about Unix shell commands can be found in the useful unix commands guide.
I rarely recommend using absolute paths in any situation. The absolute path to a file on one machine will likely be very different from the path to the file on a different machine, which could lead to errors, crashes, and unexpected behaviors in your program.
relative path
A relative path allows us to reference the location of a file or directory relative to the working directory.
Certain characters in relative paths can be use to indicate different directories relative to the working directory.
..
is used to indicate the directory one level up from the working directory.
.
is used to indicate the working directory.
~
is used to indicate your $HOME
directory.
If you do not know the location of your home directory, you can print it using the echo
command in a Unix shell.
echo $HOME
Additional information about Unix shell commands can be found in the useful unix commands guide.
cd
with relative paths
In a Unix shell, you can use the cd
command to change your working directory. Let’s say we have three directories: Level-0/Level-1/Level-2.
From Level-0, cd Level-1
will take you to Level-1.
From Level-1, cd ..
will take you to Level-0.
From Level-1, cd ./Level-2
will take you to Level-2.
From Level-2, cd ../..
will take you to Level-0.
From Level-0, cd Level-1/Level-2
will take you to Level-2.
From Level-1, cd .././Level-1/Level-2/..
will take you to Level-1.
Additional information about Unix shell commands can be found in the useful unix commands guide.
paths with spaces
When executing commands in a Unix shell, if any folder in a directory path has a space character in its name, the path must be surrounded by quotes.
cd '../directory with spaces/sub-1'
ls './directory with spaces'
mkdir 'directory with spaces'
Additional information about Unix shell commands can be found in the useful unix commands guide.
This tutorial was last updated on Thursday, May 15, 2025.