Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Absolute and relative paths on Linux

By updated

Improve this page

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:

Terminal window
cat /etc/hostname

This 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/ls

Simple 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:

Terminal window
cd /etc/ssh
cat sshd_config

Here, 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:

Terminal window
cd /tmp
cat sshd_config # Error! There is no sshd_config in /tmp
Output
cat: sshd_config: No such file or directory

Which 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:

SymbolMeaningExample
.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 the PATH variable (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 .
Terminal window
cd /tmp
cp /etc/hostname .
ls hostname
Output
hostname

The 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:

Terminal window
pwd
/var/log/nginx
Terminal window
cd ..
pwd
/var/log

You can chain several .. to climb several levels at once, then go back down into another branch of the tree:

Terminal window
cd ../../etc
pwd
/etc

From /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):

Terminal window
ls ~/.bashrc # /home/bob/.bashrc
cp file.txt ~/ # copy into /home/bob/
cd ~ # go back home

A 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:

Terminal window
cat ~/.ssh/authorized_keys
cp /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:

Terminal window
cd /etc/ssh
cd /var/log
cd - # back to /etc/ssh
Output
/etc/ssh

It 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.

SituationPreferWhy
System configuration fileAbsolute (/etc/ssh/sshd_config)The location never changes
Automated scriptAbsoluteThe script can be run from any directory
Handling nearby filesRelative (./config/app.conf)Shorter, more readable
Documentation, guidesAbsoluteNo ambiguity for the reader
Quick keyboard navigationRelative (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:

  1. The path starts with / → absolute path, resolved from the root
  2. The path starts with ~ → the shell replaces it with the home directory ($HOME, for example /home/bob, but /root for root), then the path is resolved
  3. Otherwise → relative path, resolved by the system from your current directory, the one pwd prints

Examples from /home/bob/Projects:

You typeResulting pathType
/etc/hosts/etc/hostsabsolute
~/Documents/note.txt/home/bob/Documents/note.txtabsolute (through ~)
README.md/home/bob/Projects/README.mdrelative
./src/main.py/home/bob/Projects/src/main.pyrelative
../Documents/home/bob/Documentsrelative
../../etc/etcrelative

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.

  1. Check your position:

    Terminal window
    pwd
  2. Move with an absolute path, then a relative one:

    Terminal window
    cd /var/log
    pwd
    cd ../../etc
    pwd

    You should be in /etc.

  3. Use the tilde:

    Terminal window
    cd ~
    pwd

    You are in your home directory.

  4. Copy a file with both kinds of path:

    Terminal window
    # Absolute path
    cp /etc/hostname /tmp/hostname-absolute
    # Relative path (from /etc)
    cd /etc
    cp hostname /tmp/hostname-relative

    Check:

    Terminal window
    diff /tmp/hostname-absolute /tmp/hostname-relative

    No output means the files are identical.

  5. Try cd -:

    Terminal window
    cd /var/log
    cd /etc/ssh
    cd -
    pwd

    You 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.

SymptomLikely causeFix
No such file or directoryWrong relative path (you are not where you think)Check with pwd, use an absolute path
No such file or directory with an absolute pathTypo in the pathUse Tab for completion
./script.sh: Permission deniedThe file is not executablechmod +x ./script.sh
bash: cd: /etc/ssh/sshd_config: Not a directoryYou gave cd a file instead of a directoryCheck the target is a directory with ls -ld
~ not expanded in a scriptTilde inside double or single quotesUse $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 (with cd).
  • In scripts, always prefer absolute paths or $HOME.
  • When in doubt, pwd reminds 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

6 questions
6 min
70% to pass

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