Table of Contents
Understanding Paths in Linux
In Linux, every file and directory is identified by a path. To move around the filesystem and work with files from the command line, you must understand how paths are written and interpreted. The two main kinds of paths are absolute paths and relative paths.
The Idea of the Current Directory
At any moment in a shell, you are “in” a directory. This is called your current working directory. The command pwd shows it. Many paths you type are interpreted starting from this current directory, so it is important to know where you are.
Linux also provides two special directory names that are always defined relative to your current directory. A single dot . means “this directory”. Two dots .. mean “the parent directory”, in other words, the directory that contains the current one.
When you see paths such as ./script.sh or ../Documents, they use these special names to refer to locations relative to where you currently are.
Important rule:
. means the current directory, .. means the parent directory. These special names are relative, not absolute locations.
What Is an Absolute Path?
An absolute path tells you exactly where something is in the filesystem tree, starting from the root directory /. It does not depend on your current working directory. If you give an absolute path, the system can find the file or directory even if you move to a different place in the filesystem.
Examples of absolute paths:
/home/alex/Documents/report.txt
/etc/hosts
/var/log/syslog
Every absolute path:
- Begins with
/. - Lists each directory on the way to the file or directory, separated by
/. - Is interpreted the same, no matter what your current directory is.
If your current directory is /home/alex, the path /etc/passwd still refers to the same file. If you change directory to /tmp or /, /etc/passwd still refers to that same file. That is the key property of an absolute path.
Important statement:
An absolute path always starts with / and always refers to the same location, regardless of the current directory.
What Is a Relative Path?
A relative path does not start from /. Instead, it starts from your current directory. The shell takes your current directory and then follows the relative path from there.
Examples of relative paths, assuming your current directory is /home/alex:
Documents/report.txt refers to /home/alex/Documents/report.txt.
./Documents also refers to /home/alex/Documents.
../bob refers to /home/bob, because .. is /home.
If you change your current directory, the meaning of the same relative path string can change. For example, the path Documents/report.txt only points to /home/alex/Documents/report.txt if your current directory is /home/alex. If your current directory is /home, then Documents/report.txt would refer to /home/Documents/report.txt instead, which might not even exist.
Important statement:
A relative path is interpreted starting from the current directory. The same relative path can refer to different locations if your current directory changes.
Recognizing Absolute and Relative Paths
To tell what kind of path you have, check how it begins.
If the first character is /, the path is absolute.
If it begins with any other character, such as a letter, a dot . or two dots .., it is relative.
Some simple tests:
/usr/bin/bash is absolute.
usr/bin/bash is relative.
./usr/bin/bash is relative.
../usr/bin/bash is relative.
Both usr/bin/bash and ./usr/bin/bash are relative, but the second one is written explicitly starting from the current directory with ..
Combining Paths in Your Head
To use relative paths with confidence, it helps to be able to mentally “add” them to your current directory. You can think of it like this. If your current directory is $C, and the relative path you type is $R, the full location can be thought of as
$$
\text{full\_path} = C + "/" + R
$$
After that, you must simplify by applying . and ... A . does not change anything, and a .. removes the last directory in the path.
For example, suppose your current directory is /home/alex/projects/linux. Then use this formula step by step.
If you type ../notes/todo.txt, start with
/home/alex/projects/linux + /../notes/todo.txt
which gives /home/alex/projects/linux/../notes/todo.txt.
Now remove linux/... That climbs one level up from /home/alex/projects/linux to /home/alex/projects, so you get the simplified form
/home/alex/projects/notes/todo.txt.
If you type ./scripts/../run.sh in the same directory, start with
/home/alex/projects/linux + /./scripts/../run.sh
which gives /home/alex/projects/linux/./scripts/../run.sh.
Now remove /./ because . is the same directory, and then remove scripts/.. which climbs back to /home/alex/projects/linux. The final form is
/home/alex/projects/linux/run.sh.
This way of thinking helps when you read relative paths that contain several . and .. parts.
Using `cd` with Absolute and Relative Paths
The cd command changes your current directory. It accepts both absolute and relative paths. With absolute paths, cd takes you to the same place every time. With relative paths, it depends on where you are.
If you run cd /var/log, you always end up in /var/log, no matter what your starting directory was.
If you run cd .., you always move to the parent directory of your current one. For example, if you were in /home/alex/Documents, cd .. will place you in /home/alex. If you then run cd .. again, you go to /home.
You can chain relative paths as well. If you are in /home/alex, then cd projects/linux takes you to /home/alex/projects/linux. If you are already in /home/alex/projects, cd linux is enough.
Relative paths save typing when you are working inside a certain part of the filesystem, because you do not need to repeat the full absolute path every time.
When to Prefer Absolute or Relative Paths
Absolute and relative paths each have advantages.
Absolute paths are precise and unambiguous. They are very useful in scripts or documentation, where you want to be sure that a command always refers to the same location. If a command requires a specific file that may be used from many different places, an absolute path avoids confusion.
Relative paths are shorter and more convenient while you work interactively in a terminal. If you spend time inside /home/alex/projects/linux, it is much easier to type cd docs than cd /home/alex/projects/linux/docs every time. Relative paths are most comfortable when you keep track of your current directory.
A common pattern is to use relative paths for quick navigation and file operations during interactive shell work, and to prefer absolute paths in scripts or when explaining commands to others, since absolute paths behave the same regardless of the current directory.
The Special Case of `~`
There is one more common way to write a path that looks a bit different. The tilde character ~ is expanded by the shell to a home directory. For most users, ~ means their own home directory. The exact value depends on the user, for example it might be /home/alex for a user named alex.
~ on its own expands to your home directory.
~/Documents expands to the Documents directory inside your home.
Although ~ does not start with /, the shell turns it into an absolute path before using it. That makes ~/Documents behave like an absolute path in practice.
You can see this expansion by using echo:
echo ~ prints your home directory path.
echo ~/Downloads prints the full path to your Downloads directory.
This feature is provided by the shell, not by the filesystem itself, but you will use it very often when moving around with paths.
Summary of Key Differences
Absolute paths always begin with / and always point to the same place, no matter what your current directory is. Relative paths begin from your current directory and can use . for “here” and .. for “parent”. To understand a relative path, you must always consider your current directory and then apply any . or .. components.
With these ideas, you can confidently interpret and construct paths, choose between absolute and relative paths depending on the situation, and move around the Linux filesystem from the command line.