Every Linux command that touches a file needs to know where it is. When you type cp file.conf /tmp/, the system must resolve file.conf from your current position, and /tmp/ as a fixed location on the system. Mix the two up and you copy the wrong file, or you get a No such file or directory error. This guide explains the difference between an absolute and a relative path, with the shortcuts that make paths quick to type.
What you will learn
- Tell apart an absolute path and a relative path
- Use the
.,..,~and-shortcuts to move around efficiently - Build a correct path for any command
- Avoid the most common path mistakes
What is an absolute path?
An absolute path always starts with /, the root of the filesystem. It designates a fixed location, whatever your current directory:
cat /etc/hostnameThis path works the same whether you are in /home/bob, /tmp or anywhere else. The leading / means "start from the root, then go down into etc, then open hostname".
Examples of absolute paths:
/etc/ssh/sshd_config/var/log/syslog/home/bob/Documents/report.txt/usr/bin/lsSimple rule: if the path starts with /, it is an absolute path.
What is a relative path?
A relative path does not start with /. It is interpreted from the directory you are in, the one pwd shows, so the same text can point to different files:
cd /etc/sshcat sshd_configHere, sshd_config is a relative path. It resolves to /etc/ssh/sshd_config because you are in /etc/ssh.
If you change directory, the same relative path points elsewhere:
cd /tmpcat sshd_config # Error! There is no sshd_config in /tmpcat: sshd_config: No such file or directoryWhich path shortcuts does Linux offer?
Linux provides special symbols that make paths shorter to type. The first three work in any command; the last one only with cd:
| Symbol | Meaning | Example |
|---|---|---|
. | Current directory | ./script.sh (run a script here) |
.. | Parent directory (one level up) | cd .. (go up) |
~ | Your home directory ($HOME, for example /home/bob) | cd ~ or ls ~/Documents |
- | Previous directory, with cd (with other commands, a lone - means something else) | cd - (go back) |
.: the current directory
A single dot means "here". It is mostly useful to run a script that sits in the current directory, and to name the destination of a copy:
- Run a script in the current directory:
./my-script.sh. For a command without a/, the shell only searches the directories listed in thePATHvariable (a list of directories, such as/usr/bin), and the current directory is not part of it by default:./says that the script is here and that there is nothing to search for. - Copy a file here:
cp /etc/hostname .
cd /tmpcp /etc/hostname .ls hostnamehostnameThe file was copied into /tmp/, the current directory.
..: the parent directory
The double dot goes up one level in the tree. Each .. climbs one directory, whatever its name:
pwd/var/log/nginxcd ..pwd/var/logYou can chain several .. to climb several levels at once, then go back down into another branch of the tree:
cd ../../etcpwd/etcFrom /var/log, ../../etc means: go up two levels (to /), then go down into etc.
~: your home
The tilde is a shortcut to the home directory of the logged-in user. For the user bob, ~ equals /home/bob (the value of $HOME; for root, it is /root):
ls ~/.bashrc # /home/bob/.bashrccp file.txt ~/ # copy into /home/bob/cd ~ # go back homeA point worth remembering, because it often surprises people: the tilde
does not belong to cd. The shell replaces it before the command even
runs, so it works everywhere:
cat ~/.ssh/authorized_keyscp /etc/ssh/sshd_config ~/backup/mkdir -p ~/Projects/lab-linux-: back to the previous directory
With cd, the dash takes you back to the last directory visited, and prints its path on the way:
cd /etc/sshcd /var/logcd - # back to /etc/ssh/etc/sshIt is handy when you switch between two places, such as a configuration and the log of the service it drives. This shortcut belongs to cd: with other commands, a lone - keeps another meaning, for example cat - reads the standard input (what you type or what a pipe sends it). cp - or ls - do not go back to the previous directory.
Absolute or relative: which one should you use?
The choice depends on who will use the path and from where. A path typed at the keyboard can be relative; a path written in a script or a configuration file must work from anywhere.
| Situation | Prefer | Why |
|---|---|---|
| System configuration file | Absolute (/etc/ssh/sshd_config) | The location never changes |
| Automated script | Absolute | The script can be run from any directory |
| Handling nearby files | Relative (./config/app.conf) | Shorter, more readable |
| Documentation, guides | Absolute | No ambiguity for the reader |
| Quick keyboard navigation | Relative (cd .., cd src/) | Faster to type |
How does the shell resolve a path?
When you type a command with a path, the shell and the system proceed in three steps, in this order. Knowing them lets you predict where a path lands before you press Enter:
- The path starts with
/→ absolute path, resolved from the root - The path starts with
~→ the shell replaces it with the home directory ($HOME, for example/home/bob, but/rootfor root), then the path is resolved - Otherwise → relative path, resolved by the system from your current directory, the one
pwdprints
Examples from /home/bob/Projects:
| You type | Resulting path | Type |
|---|---|---|
/etc/hosts | /etc/hosts | absolute |
~/Documents/note.txt | /home/bob/Documents/note.txt | absolute (through ~) |
README.md | /home/bob/Projects/README.md | relative |
./src/main.py | /home/bob/Projects/src/main.py | relative |
../Documents | /home/bob/Documents | relative |
../../etc | /etc | relative |
Practical exercise
These steps replay both kinds of path on real directories, without changing anything important: the copies go to /tmp. Run each check before moving on, it is what turns the rules above into a reflex.
-
Check your position:
Terminal window pwd -
Move with an absolute path, then a relative one:
Terminal window cd /var/logpwdcd ../../etcpwdYou should be in
/etc. -
Use the tilde:
Terminal window cd ~pwdYou are in your home directory.
-
Copy a file with both kinds of path:
Terminal window # Absolute pathcp /etc/hostname /tmp/hostname-absolute# Relative path (from /etc)cd /etccp hostname /tmp/hostname-relativeCheck:
Terminal window diff /tmp/hostname-absolute /tmp/hostname-relativeNo output means the files are identical.
-
Try cd -:
Terminal window cd /var/logcd /etc/sshcd -pwdYou should be back in
/var/log.
Troubleshooting
Path errors all look alike on screen: a command that fails on a file you are
sure exists. The table sorts them by exact message, because that is the
only thing you have at the moment of the problem. In every case, the first
reflex stays the same: run pwd to know from which directory your relative paths are resolved.
| Symptom | Likely cause | Fix |
|---|---|---|
No such file or directory | Wrong relative path (you are not where you think) | Check with pwd, use an absolute path |
No such file or directory with an absolute path | Typo in the path | Use Tab for completion |
./script.sh: Permission denied | The file is not executable | chmod +x ./script.sh |
bash: cd: /etc/ssh/sshd_config: Not a directory | You gave cd a file instead of a directory | Check the target is a directory with ls -ld |
~ not expanded in a script | Tilde inside double or single quotes | Use $HOME instead of ~ in scripts |
Put it into practice
A relative path does not point at the same file depending on where you stand: the troubleshooting table shows it. The lab prepares a separate working folder on your computer, with two files, source.txt and puzzles.txt. The commands act on your own machine. You copy source.txt twice, first writing the destination path as an absolute path, then as a relative one; the backup/absolute and backup/relative folders do not exist yet, so you have to create them. You then solve five puzzles in puzzles.txt: for each one, a starting point and a target file are given, and you write the relative path that leads from one to the other. Validation checks that both copies exist and compares each answer with the expected path.
Key points
- An absolute path starts with
/and works from any directory. - A relative path depends on your current position (
pwd). .= here,..= parent,~= home ($HOME),-= previous (withcd).- In scripts, always prefer absolute paths or
$HOME. - When in doubt,
pwdreminds you where you are, and Tab completes paths.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Hard links and symbolic links on Linux
A new full quiz with random questions
Work again only on the questions you missed