Navigating the Unix Filesystem
Shell·21 min read·Jan 1, 2025
In Unix-like operating systems all data is organized into files and all files are organized into folders called directories.
These directories are arranged in a hierarchical tree-like structure called the filesystem.
The very first directory of the filesystem is called the root directory, as it is the one containing the entire tree of subdirectories and files of your computer.
root
└── directory
├── file
├── directory
│ └── file
└── ...
Note: The root directory is usually represented by a slash character
/.
Within the filesystem, the location of each file and directory is identified by a unique string of characters called a pathname (or simply path), which can be used as argument for various shell commands.
Absolute paths
An absolute path is the full path to a file or directory starting from the root directory.
The syntax of absolute paths
An absolute path always starts with a forward slash / representing the root directory, followed by a sequence of directory names separated by forward slashes /, leading directly to the target file or directory.
For example:
/directory1/directory2/.../file
Example
For example, let's consider this filesystem representation, where the root directory / contains the johndoe directory, which contains the Downloads directory, which contains the ebook.pdf file:
/
└── home
└── johndoe
└── Downloads
└── ebook.pdf
To write the absolute path to the ebook.pdf file, we start by writing a forward slash character / representing the root directory:
/
Then we write the name of each directory, from top to bottom, until the one containing the file:
/home/johndoe/Downloads
And finally we write the name of the file itself:
/home/johndoe/Downloads/ebook.pdf
Relative paths
A relative path is the path to a file or directory relative to the current working directory.
The current working directory
The current working directory, often abbreviated CWD, is the location in the filesystem at a given time in which commands are executed.
For example, when launching a new terminal window, the current working directory is usually set to your home directory by default.
The home directory
The home directory is the directory designated for each user on the system that serves as the main location for storing a user's personal files, scripts, directories, settings, and other data.
On Linux, the home directory is by default set to /home/username and on macOS to /Users/username.
For example, the home directory of the user named john would typically be located at /home/john on Linux and /Users/john on macOS.
Note: When writing relative paths, the tilde character
~is a shortcut to the home directory.
The syntax of relative paths
A relative path always starts with either:
-
A file name.
-
A directory name.
-
A single dot
.representing the current directory. -
A double dot
..representing the parent directory.
It is then optionally followed by a sequence of directory names separated by forward slash characters /, leading directly to the target file or directory.
For example:
../directoryA/directoryB/file
Relative forward paths
A relative forward path is the path to a file or directory that is located in the current working directory or in a subdirectory of the current working directory.
Example
Let's consider this filesystem representation, in which the current working directory is the home directory located at /home/johndoe:
/
└── home
└── johndoe ← Current working directory
└── Downloads
└── ebook.pdf ← Target file
To write the relative forward path to the ebook.pdf file, we start by writing a dot . representing the current directory:
.
Then we write the name of each directory until the one containing the target file:
./Downloads
And finally we write the name of the file itself:
./Downloads/ebook.pdf
Note: Since the dot
.represents the current working directory, the relative path forward to theebook.pdffile can also be written as follows:Downloads/ebook.pdf
Relative backward paths
A relative backward path is the path to a file or directory that is located in one of the parent directories of the current working directory or at the same level in the filesystem.
Example
Let's consider this filesystem representation, in which the current working directory is the Downloads directory located at /home/johndoe/Downloads:
/
└── home
└── johndoe
├── Documents
│ └── Invoices
│ └── invoice.pdf ← Target file
└── Downloads ← Current working directory
└── ebook.pdf
To write the relative backward path to the invoice.pdf file, we start by writing a double dot .. representing the parent directory of the current working directory, which is johndoe:
..
Then we write the name of each directory until the one containing the target file:
../Documents/Invoices
And finally we write the name of the file itself:
../Documents/Invoices/invoice.pdf
Printing the CWD
To output the absolute path to the current working directory, you can use the pwd command — which stands for print working directory:
$ pwd
Example
For example, if you launch a new terminal window and run the pwd command, you should see appearing the path to your home directory:
$ pwd
/home/username
Changing the CWD
To change the current working directory, which means navigating into another directory, you can use the cd builtin — which stands for change directory:
$ cd [path]
Where path is the relative or absolute path to the target directory.
Notes:
If the specified path doesn't match a valid location in the filesystem, the
cdcommand will output the following error message:-bash: cd: path: No such file or directoryExecuting the
cdcommand without arguments allows you to quickly navigate to your home directory, which is equivalent tocd ~.
Example
Let's consider this filesystem representation, in which the current working directory is the /home/johndoe directory:
/
└── home
└── johndoe ← Current working directory
└── Downloads
Executing this command will change the current working directory to /home/johndoe/Downloads:
$ cd ./Downloads
As shown by the output of the pwd command:
$ pwd
/home/johndoe/Downloads
And executing this command will change the current working directory to the parent directory /home/johndoe:
$ cd ..
As shown by the output of the pwd command:
$ pwd
/home/johndoe
Navigating to the previous CWD
To quickly navigate to the previous working directory, you can use the cd command with the hyphen character -:
$ cd -
Where - is a shortcut to the previous working directory.
Example
Let's consider this filesystem representation, in which the current working directory is the /home/johndoe/Downloads/Invoices directory:
/
└── home
└── johndoe
└── Downloads
└── Invoices ← Current working directory
Executing this command will change the current working directory to /home/johndoe:
$ cd
As shown by the output of the pwd command:
$ pwd
/home/johndoe
And executing this command will change the current working directory to /home/johndoe/Downloads/Invoices:
$ cd -
As shown by the output of the pwd command:
$ pwd
/home/johndoe/Downloads/Invoices
🗒️ Summary
Here's a summary of what you've learned in this lesson:
-
An absolute path is composed from the root directory upwards and always starts with a slash
/symbolizing the root directory. -
A relative path is composed from the current working directory and always starts with a filename, a dot
., or a double dot... -
Each component of a path name must be separated by exactly one slash character
/. -
A single dot
.represents the current working directory. -
A double dot
..represents the parent directory. -
The
pwdcommand outputs the path of the current working directory. -
The
cdcommand changes the current working directory.
Unlock the CLI & Scripting with Bash module
Learn how to gain advanced control over the OS and automate complex routine tasks prone to manual errors with the CLI and Bash scripting.
You get immediate access to:
- 34 focused lessons across the CLI and Bash
- 4 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle