Absolute vs relative paths in Linux CLI - Performance Comparison
When working with file paths in Linux, commands often use absolute or relative paths to find files.
We want to understand how the time to locate a file changes depending on the path type used.
Analyze the time complexity of the following commands.
# Using absolute path
cat /home/user/documents/report.txt
# Using relative path
cat documents/report.txt
These commands read a file using either an absolute or a relative path.
Look at what the system does to find the file.
- Primary operation: Traversing directories in the path to locate the file.
- How many times: Once per directory in the path, step by step.
Finding a file means checking each folder in the path one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 (folders deep) | 3 directory lookups |
| 5 (folders deep) | 5 directory lookups |
| 10 (folders deep) | 10 directory lookups |
Pattern observation: The time grows linearly with the number of folders in the path.
Time Complexity: O(n)
This means the time to find a file grows in direct proportion to how many folders are in its path.
[X] Wrong: "Using an absolute path is always slower because it starts from the root."
[OK] Correct: Both absolute and relative paths require checking each folder in the path, so time depends on path length, not path type.
Understanding how file paths affect command execution helps you reason about system behavior and efficiency in real tasks.
What if the file system used caching for directory lookups? How would that affect the time complexity?
Practice
Solution
Step 1: Understand absolute path definition
An absolute path always starts from the root directory, which is represented by/.Step 2: Identify the path starting with
Only//home/user/documentsstarts with/, making it an absolute path.Final Answer:
/home/user/documents -> Option BQuick Check:
Absolute path starts with / = A [OK]
- Confusing relative paths with absolute paths
- Thinking ./ or ../ are absolute paths
- Ignoring the leading slash in paths
projects inside the current folder?Solution
Step 1: Understand relative path usage in cd
To move into a folder inside the current directory, you can use the folder name directly or prefix it with./.Step 2: Identify the simplest correct syntax
cd projectsusescd projects, which is correct and simplest for relative path inside current folder.Final Answer:
cd projects -> Option AQuick Check:
Relative path inside current folder = cd projects [OK]
- Using leading slash for relative paths
- Confusing ../ with current directory
- Adding unnecessary ./ prefix
/home/user, what is the full absolute path of ../documents/report.txt?Solution
Step 1: Understand
..in relative paths..means move one directory up from current directory. From/home/user, going up one level is/home.Step 2: Append the rest of the path
After moving up, appenddocuments/report.txtto get/home/documents/report.txt.Final Answer:
/home/documents/report.txt -> Option CQuick Check:
.. moves up one folder = /home/documents/report.txt [OK]
- Not moving up directory with ..
- Confusing relative and absolute paths
- Leaving .. in the final path
cd ../folder but get an error: bash: cd: ../folder: No such file or directory. What is the likely cause?Solution
Step 1: Understand the error message
The error says the directory../folderdoes not exist, meaning the folder namedfolderis missing in the parent directory.Step 2: Check the path usage
The command uses relative path correctly. The problem is the folder is not found at that location.Final Answer:
The folderfolderdoes not exist inside the parent directory -> Option AQuick Check:
No such file means folder missing at relative path [OK]
- Assuming syntax error instead of missing folder
- Confusing relative and absolute paths
- Trying to use ./ instead of ../ incorrectly
/var/www/html. You want to copy a file index.html from /home/user/website to your current directory using a relative path. Which command is correct?Solution
Step 1: Calculate relative path from
From/var/www/htmlto/home/user/website/var/www/html, going up three levels:html -> www -> varreaches/. Then go intohome/user/website.Step 2: Form the relative path
The relative path is../../../home/user/website/index.html. So the command iscp ../../../home/user/website/index.html ..Final Answer:
cp ../../../home/user/website/index.html . -> Option DQuick Check:
3 ups to root, then home path = ../../../home/user/website [OK]
- Using too few ../ and missing root
- Using absolute path when asked for relative
- Confusing current directory (.) with parent (..)
Ready to prove it?
Pass the test and this pattern moves unknown
