What is the difference between absolute and relative paths in Linux?
Learn the difference between absolute and relative paths in Linux, how the working directory, . and .. work, and when to use each for reliable navigation.
Expected Interview Answer
An absolute path specifies a file's full location from the root directory and always begins with a slash (/), so it is the same regardless of where you are; a relative path describes a location starting from your current working directory and never begins with a slash.
Absolute paths like /home/alice/notes.txt are unambiguous and work from anywhere, which makes them ideal for scripts, cron jobs and configuration. Relative paths like ../docs/notes.txt depend entirely on the current directory (checked with pwd) and use shortcuts such as . for the current directory and .. for the parent. Relative paths are shorter and portable across similar structures, but they resolve differently as you move around the filesystem.
- Absolute paths are unambiguous from any location
- Relative paths are shorter to type
- Relative paths stay valid if a whole tree is moved
- Absolute paths are safer in scripts and cron
- Clarifies how . and .. navigate the tree
AI Mentor Explanation
An absolute path is like giving a stadium's full postal address — country, city, street — so anyone anywhere can find it. A relative path is like telling a teammate already at the ground 'the nets are two gates left of where you stand.' The address works from anywhere; the directions only make sense from your current spot on the field.
Step-by-Step Explanation
Step 1
Check where you are
Run pwd to print the current working directory, which anchors every relative path you type.
Step 2
Recognise absolute paths
Any path starting with / is absolute and resolved from the root, independent of the current directory.
Step 3
Recognise relative paths
Any path not starting with / is relative and joined onto the current working directory.
Step 4
Use the navigation shortcuts
Use . for the current directory, .. for the parent and ~ for your home directory to move around concisely.
Step 5
Choose deliberately
Prefer absolute paths in scripts and cron for reliability, and relative paths for quick interactive work within a project.
What Interviewer Expects
- Absolute paths start with / and relative paths do not
- Understands the current working directory and pwd
- Knows the meaning of ., .. and ~
- Explains why scripts favour absolute paths
- Can trace how a relative path resolves
Common Mistakes
- Thinking ~ is an absolute path (it is a shortcut expanded to one)
- Assuming relative paths work the same from any directory
- Using relative paths in cron jobs and getting failures
- Confusing . (current) with .. (parent)
- Forgetting a leading / makes a path absolute
Best Answer (HR Friendly)
“An absolute path gives a file's full address from the top of the filesystem, so it works no matter where you are. A relative path gives directions from your current folder, which is shorter but only makes sense from where you currently stand.”
Code Example
# Show current directory
pwd
# /home/alice/project
# Absolute path: works from anywhere
cat /home/alice/project/README.md
# Relative path: resolved from /home/alice/project
cat README.md
# .. moves up one level, then into a sibling folder
cat ../notes/todo.txt
# ~ expands to the home directory
cat ~/project/README.mdFollow-up Questions
- What do . and .. represent in a path?
- Is ~ an absolute or relative path?
- Why should cron jobs use absolute paths?
- How does the shell resolve a relative path?
- What does the cd command do with each type of path?
MCQ Practice
1. Which of these is an absolute path?
An absolute path begins with / and is resolved from the root, so /etc/hosts qualifies.
2. What does .. refer to in a relative path?
.. always refers to the parent of the current directory, moving one level up the tree.
3. Why are absolute paths preferred inside cron jobs?
Cron often runs with an unpredictable working directory, so relative paths can resolve incorrectly while absolute paths stay reliable.
Flash Cards
How do you spot an absolute path? — It begins with a leading slash (/).
What anchors a relative path? — The current working directory, shown by pwd.
What does .. mean? — The parent of the current directory.
Is ~ absolute? — No, it is a shortcut the shell expands to the absolute home directory path.
When prefer absolute paths? — In scripts and cron jobs, where the working directory is uncertain.