Bird
Raised Fist0
Linux CLIscripting~10 mins

Absolute vs relative paths in Linux CLI - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Absolute vs relative paths
Start in current directory
↓
Use relative path?
Yes→Look inside current directory or subfolder
No↓
Use absolute path?
→Look from root directory /
↓
Access file/folder
This flow shows how the system decides to find files using either relative paths from the current folder or absolute paths from the root.
Execution Sample
Linux CLI
pwd
ls ./documents
ls /home/user/documents
Shows current directory, lists files in 'documents' folder relative to current directory, then lists files using absolute path.
Execution Table
StepCommandPath TypeResolved PathActionOutput
1pwdN/A/home/userShow current directory/home/user
2ls ./documentsRelative/home/user/documentsList files in relative pathfile1.txt file2.txt
3ls /home/user/documentsAbsolute/home/user/documentsList files in absolute pathfile1.txt file2.txt
4ls ../user2Relative/home/user2List files in sibling directoryproject.doc notes.txt
5ls /etcAbsolute/etcList files in root etc folderhosts passwd
6ls ./nonexistentRelative/home/user/nonexistentError: folder does not existls: cannot access './nonexistent': No such file or directory
7ls /nonexistentAbsolute/nonexistentError: folder does not existls: cannot access '/nonexistent': No such file or directory
💡 Commands stop after showing outputs or errors for given paths.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
Current Directory/home/user/home/user/home/user/home/user/home/user/home/user/home/user/home/user
Path UsedN/AN/A./documents/home/user/documents../user2/etc./nonexistent/nonexistent
Resolved PathN/A/home/user/home/user/documents/home/user/documents/home/user2/etc/home/user/nonexistent/nonexistent
Command OutputN/A/home/userfile1.txt file2.txtfile1.txt file2.txtproject.doc notes.txthosts passwdls: cannot access './nonexistent': No such file or directoryls: cannot access '/nonexistent': No such file or directory
Key Moments - 3 Insights
Why does 'ls ./documents' and 'ls /home/user/documents' show the same files?
Because './documents' is a relative path from the current directory '/home/user', which resolves to '/home/user/documents', the same as the absolute path.
What happens if the relative path points outside the current directory?
Using '../user2' moves up one directory from '/home/user' to '/home' then into 'user2', showing files there as in step 4.
Why do some 'ls' commands give errors?
Because the specified folder does not exist at the resolved path, so the system cannot list files, as shown in steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the resolved path for 'ls ../user2'?
A/user2
B/home/user/user2
C/home/user2
D/home/user/documents
💡 Hint
Check the 'Resolved Path' column at step 4 in the execution table.
At which step does the command fail because the folder does not exist?
AStep 2
BStep 6
CStep 3
DStep 5
💡 Hint
Look for error messages in the 'Output' column in the execution table.
If the current directory was '/home/user/documents', what would 'ls ../' list?
AFiles in /home/user
BFiles in /home/user/documents
CFiles in /home
DFiles in /
💡 Hint
Relative path '..' means one directory up from current directory; check variable_tracker for current directory.
Concept Snapshot
Absolute paths start from root '/' and show full location.
Relative paths start from current directory and use './' or '../'.
Use absolute paths for exact location.
Use relative paths for location relative to where you are.
Errors occur if path does not exist.
Commands like 'ls' show files at resolved paths.
Full Transcript
This lesson shows how Linux finds files using absolute and relative paths. Absolute paths start from the root directory '/' and give the full location. Relative paths start from the current directory and use './' for current folder or '../' to go up one folder. Commands like 'ls' list files at the resolved location. If the path does not exist, an error appears. For example, 'ls ./documents' lists files inside 'documents' folder relative to current directory, while 'ls /home/user/documents' uses the full absolute path. Using '../user2' moves up one folder and then into 'user2'. Understanding these paths helps you find files easily in the terminal.

Practice

(1/5)
1. Which of the following is an absolute path in Linux?
easy
A. documents/reports
B. /home/user/documents
C. ./downloads
D. ../pictures

Solution

  1. Step 1: Understand absolute path definition

    An absolute path always starts from the root directory, which is represented by /.
  2. Step 2: Identify the path starting with /

    Only /home/user/documents starts with /, making it an absolute path.
  3. Final Answer:

    /home/user/documents -> Option B
  4. Quick Check:

    Absolute path starts with / = A [OK]
Hint: Absolute paths always start with a slash / [OK]
Common Mistakes:
  • Confusing relative paths with absolute paths
  • Thinking ./ or ../ are absolute paths
  • Ignoring the leading slash in paths
2. Which command correctly changes directory to a relative path named projects inside the current folder?
easy
A. cd projects
B. cd .projects
C. cd /projects
D. cd ../projects

Solution

  1. 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 ./.
  2. Step 2: Identify the simplest correct syntax

    cd projects uses cd projects, which is correct and simplest for relative path inside current folder.
  3. Final Answer:

    cd projects -> Option A
  4. Quick Check:

    Relative path inside current folder = cd projects [OK]
Hint: Use folder name alone to cd into relative path [OK]
Common Mistakes:
  • Using leading slash for relative paths
  • Confusing ../ with current directory
  • Adding unnecessary ./ prefix
3. If your current directory is /home/user, what is the full absolute path of ../documents/report.txt?
medium
A. /documents/report.txt
B. /home/user/documents/report.txt
C. /home/documents/report.txt
D. /home/user/../documents/report.txt

Solution

  1. Step 1: Understand .. in relative paths

    .. means move one directory up from current directory. From /home/user, going up one level is /home.
  2. Step 2: Append the rest of the path

    After moving up, append documents/report.txt to get /home/documents/report.txt.
  3. Final Answer:

    /home/documents/report.txt -> Option C
  4. Quick Check:

    .. moves up one folder = /home/documents/report.txt [OK]
Hint: .. means go up one folder from current directory [OK]
Common Mistakes:
  • Not moving up directory with ..
  • Confusing relative and absolute paths
  • Leaving .. in the final path
4. You run cd ../folder but get an error: bash: cd: ../folder: No such file or directory. What is the likely cause?
medium
A. The folder folder does not exist inside the parent directory
B. You used an absolute path instead of relative
C. You need to use ./folder instead
D. You must use cd /folder to access it

Solution

  1. Step 1: Understand the error message

    The error says the directory ../folder does not exist, meaning the folder named folder is missing in the parent directory.
  2. Step 2: Check the path usage

    The command uses relative path correctly. The problem is the folder is not found at that location.
  3. Final Answer:

    The folder folder does not exist inside the parent directory -> Option A
  4. Quick Check:

    No such file means folder missing at relative path [OK]
Hint: Check if folder exists in parent directory when using ../folder [OK]
Common Mistakes:
  • Assuming syntax error instead of missing folder
  • Confusing relative and absolute paths
  • Trying to use ./ instead of ../ incorrectly
5. You are in /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?
hard
A. cp /home/user/website/index.html .
B. cp ../../home/user/website/index.html .
C. cp ../../../../home/user/website/index.html .
D. cp ../../../home/user/website/index.html .

Solution

  1. Step 1: Calculate relative path from /var/www/html to /home/user/website

    From /var/www/html, going up three levels: html -> www -> var reaches /. Then go into home/user/website.
  2. Step 2: Form the relative path

    The relative path is ../../../home/user/website/index.html. So the command is cp ../../../home/user/website/index.html ..
  3. Final Answer:

    cp ../../../home/user/website/index.html . -> Option D
  4. Quick Check:

    3 ups to root, then home path = ../../../home/user/website [OK]
Hint: Count folders up to root, then add full path [OK]
Common Mistakes:
  • Using too few ../ and missing root
  • Using absolute path when asked for relative
  • Confusing current directory (.) with parent (..)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard