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.
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:
cat /home/learner/project/docs/tasks.txtBuy milk
Call the bank
Read chapter 3
Buy bread
Water the plantsThe 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:
cd ~/project
cat docs/tasks.txtBuy milk
Call the bank
Read chapter 3
Buy bread
Water the plantsThe 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":
cd ~/project/src
pwd
ls ../docs
cd ../..
pwd/home/learner/project/src
notes.txt
tasks.txt
/home/learnerAnd ~ works from any directory, so ~/project/README.md always refers to that one file:
cd /tmp
cat ~/project/README.mdProject 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 ./:
cd ~/project/src
./hello.shHello from a scriptWithout 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:
cd ~/project/src
realpath ../docs/notes.txt/home/learner/project/docs/notes.txtpwd gives the absolute path of where you are, and basename and dirname split a path into its file name and its folder:
basename /home/learner/project/docs/notes.txt
dirname /home/learner/project/docs/notes.txtnotes.txt
/home/learner/project/docsWhich 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.
Ready for the next small win?
Mark the lesson complete when you are ready.