Skip to lesson

Linux Command Line · Lesson 7 of 43

Absolute and relative paths

Understand absolute and relative paths in Linux, how ., .. and ~ work, and how to choose the right one when moving or naming files.

Beginner to intermediate·11 min·Navigating the filesystem·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

Every command that works with files needs you to say where those files are. You will learn the two ways of saying it, how to move between them without thinking, and how to avoid the classic mistake of running a command in the wrong place.

Absolute paths

An absolute path starts with / and describes the full route from the root of the filesystem. It names the same place no matter where you are:

bash
cat /home/learner/project/docs/tasks.txt
result
Buy milk
Call the bank
Read chapter 3
Buy bread
Water the plants

The leading slash is what makes it absolute. Read it as: start at the root, go into home, then learner, then project, then docs, then the file tasks.txt.

Relative paths

A relative path does not start with /. It is read from your current directory. If you are already in the project folder, the same file is simply docs/tasks.txt:

bash
cd ~/project
cat docs/tasks.txt
result
Buy milk
Call the bank
Read chapter 3
Buy bread
Water the plants

The same relative path gives a different result from a different folder, which is both the strength and the danger of relative paths: they are short, but they depend on where you are.

Three special names

Every directory contains two hidden entries that act as signposts, and the shell adds a third:

  • . is the current directory.
  • .. is the parent directory, one level up.
  • ~ is your home directory, expanded by the shell.

You can use them anywhere a path is expected, and combine them. If you are in src, then ../docs means "up one level, then into docs":

bash
cd ~/project/src
pwd
ls ../docs
cd ../..
pwd
result
/home/learner/project/src
notes.txt
tasks.txt
/home/learner

And ~ works from any directory, so ~/project/README.md always refers to that one file:

bash
cd /tmp
cat ~/project/README.md
result
Project folder

A small folder used to practise the command line.

Why ./ matters

When you type a command name, the shell searches the directories in your PATH. The current directory is deliberately not in it, for safety. To run a program that sits in the current folder, say so explicitly with ./:

bash
cd ~/project/src
./hello.sh
result
Hello from a script

Without the leading ./, the shell would answer hello.sh: command not found, even though the file is right there. You will see this again in the scripting lessons.

Converting between the two

realpath takes a relative path and prints the absolute one, resolving . and .. along the way:

bash
cd ~/project/src
realpath ../docs/notes.txt
result
/home/learner/project/docs/notes.txt

pwd gives the absolute path of where you are, and basename and dirname split a path into its file name and its folder:

bash
basename /home/learner/project/docs/notes.txt
dirname /home/learner/project/docs/notes.txt
result
notes.txt
/home/learner/project/docs

Which one to use

Use relative paths for quick work in the folder you are in, where short names save typing. Use absolute paths in scripts, scheduled jobs and anywhere the starting directory is not guaranteed, because a job started by cron does not begin where you expect. Starting a path with ~ is a good middle ground for files in your home folder.

Common mistakes

Running a command in the wrong folder. Check pwd before commands that change or delete files.

Forgetting the first slash. home/learner is relative and fails from most places, while /home/learner works from anywhere.

Typing ~ inside quotes. "~/file" does not expand because the shell skips tilde expansion inside quotes. Use "$HOME/file" instead.

Assuming .. stays inside your project. cd ../../.. can take you far out of it. Check with pwd.

Exercise

From your home folder, write down both the absolute and the relative path of a file inside a subfolder two levels down. Then cd into that subfolder and reach a file in your home folder using only ... Use realpath to check your answer. Finally, try running a script in your current folder with and without the leading ./ and note the difference.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 Which of these is an absolute path?
02 You are in /home/learner/project/src. Where does cd ../docs lead?
03 What does ./run.sh mean?
04 Why might a script prefer absolute paths?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses