Bird
Raised Fist0
Linux CLIscripting~5 mins

Absolute vs relative paths in Linux CLI - CLI Comparison

Choose your learning style10 modes available
Introduction
When you work with files and folders on your computer, you need to tell the system where to find them. Absolute and relative paths are two ways to do this. Absolute paths give the full address from the root, while relative paths give the address from where you currently are.
When you want to open a file from anywhere on your system without confusion.
When you are inside a folder and want to access a file nearby without typing the full address.
When writing scripts that need to work regardless of where they are run from.
When moving between folders in the terminal and running commands on files.
When linking files or folders in configuration files or shortcuts.
Commands
Shows your current folder's full path, so you know where you are in the system.
Terminal
pwd
Expected OutputExpected
/home/user/projects
Lists files in the /etc folder using an absolute path, starting from the root.
Terminal
ls /etc
Expected OutputExpected
passwd hosts hostname
Lists files in the documents folder one level up from your current folder using a relative path.
Terminal
ls ../documents
Expected OutputExpected
resume.pdf notes.txt
Displays the content of the hostname file using an absolute path to avoid confusion.
Terminal
cat /etc/hostname
Expected OutputExpected
my-computer
Shows the content of notes.txt using a relative path from your current folder.
Terminal
cat ../documents/notes.txt
Expected OutputExpected
Meeting at 10am Buy groceries
Key Concept

If you remember nothing else from this pattern, remember: absolute paths start from the root and always point to the same place, while relative paths start from your current folder and change depending on where you are.

Common Mistakes
Using a relative path without knowing your current folder.
The command fails or accesses the wrong file because the starting point is different than expected.
Use pwd to check your current folder before using relative paths, or use absolute paths for certainty.
Typing an absolute path but missing the leading slash (/).
The system treats it as a relative path and looks in the wrong place.
Always start absolute paths with a slash to indicate the root folder.
Summary
Use pwd to find your current folder's absolute path.
Absolute paths start with / and always point to the same location.
Relative paths depend on your current folder and use ./ or ../ to move around.

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