Absolute and relative paths

2.3 Using Directories and Listing Files (Weight: 2)

📘Linux Essentials (LPI 010-160)


In Linux systems, files and directories are organized in a hierarchical directory structure. Every file and directory has a path, which tells the system where the file or directory is located.

Understanding absolute paths and relative paths is very important for the Linux Essentials (LPI 010-160) exam because many commands require a path to locate files and directories.

This section explains both types of paths, how they work, and how they are used in a typical Linux environment.


1. What is a Path?

A path is the address of a file or directory in the Linux filesystem.

Linux organizes files in a tree structure that starts from a single top-level directory called the root directory.

The root directory is represented by:

/

From this root directory, all other directories branch out.

Example directory structure:

/
├── home
│ └── student
│ └── documents
│ └── report.txt
├── etc
└── var

The file report.txt is inside:

/home/student/documents/

The path describes exactly where the file is located.

There are two ways to specify a path:

  1. Absolute path
  2. Relative path

2. Absolute Paths

Definition

An absolute path is the complete path to a file or directory starting from the root directory (/).

It always begins with a forward slash (/).

An absolute path shows the full location of the file regardless of the current directory.


Example of an Absolute Path

/home/student/documents/report.txt

Breakdown:

PartMeaning
/Root directory
homeDirectory inside root
studentUser directory
documentsSubdirectory
report.txtFile

This path tells Linux exactly where the file is located.


Characteristics of Absolute Paths

Absolute paths:

  • Always start with /
  • Provide the full location of a file
  • Work from any directory
  • Are commonly used in scripts, configuration files, and system administration

Example Commands Using Absolute Paths

Listing a directory

ls /home/student/documents

Viewing a file

cat /home/student/documents/report.txt

Changing directory

cd /home/student/documents

Even if the user is in another directory, these commands still work because the full path is specified.


IT Environment Example

In a Linux server environment:

System configuration files are usually stored in:

/etc

Example:

/etc/ssh/sshd_config

Administrators often use absolute paths when editing configuration files to avoid confusion.

Example:

sudo nano /etc/ssh/sshd_config

This ensures the correct file is opened.


3. Relative Paths

Definition

A relative path specifies the location of a file or directory relative to the current working directory.

It does not start with /.

Instead, it describes how to reach a file from the directory you are currently in.


Current Working Directory

The current working directory is the directory where the user is currently operating.

To check the current directory, use:

pwd

Example output:

/home/student

This means the user is currently inside:

/home/student

Example Directory Structure

/home/student
├── documents
│ └── report.txt
└── downloads

Current directory:

/home/student

To access report.txt, the relative path would be:

documents/report.txt

Example Commands Using Relative Paths

Listing a directory

ls documents

Viewing a file

cat documents/report.txt

Changing directory

cd documents

These commands work because the documents directory exists inside the current directory.


4. Special Relative Path Symbols

Linux provides special directory references for relative paths.

These are important for navigation.


Current Directory (.)

The symbol:

.

represents the current directory.

Example:

ls .

This lists files in the current directory.

Example:

./script.sh

This runs a script located in the current directory.

This is commonly used when executing scripts that are not in the system PATH.


Parent Directory (..)

The symbol:

..

represents the parent directory.

Example structure:

/home/student/documents

Current directory:

documents

Using:

cd ..

moves to:

/home/student

Moving Multiple Levels Up

You can use multiple .. symbols.

Example:

cd ../../

This moves two directories up.


5. Combining Relative Paths

Relative paths can combine directory names and special symbols.

Example directory structure:

/home/student/projects/linux/scripts

Current directory:

/home/student/projects/linux

To access the scripts directory:

cd scripts

To access a file:

scripts/install.sh

To move back to projects:

cd ..

To reach /home/student:

cd ../..

6. Absolute vs Relative Paths

FeatureAbsolute PathRelative Path
Starting pointRoot directory /Current directory
Starts with /YesNo
Depends on current directoryNoYes
Full locationYesNo
Common usageSystem commands, scripts, configurationNavigation inside directories

7. When Absolute Paths Are Preferred

Absolute paths are preferred when:

  • Writing shell scripts
  • Editing system configuration files
  • Running administrative commands
  • Working with system directories
  • Avoiding path confusion

Example:

/var/log/syslog

Using an absolute path ensures the correct file is accessed.


8. When Relative Paths Are Preferred

Relative paths are useful when:

  • Navigating within a project directory
  • Working inside a user directory
  • Running scripts from the current folder
  • Performing quick file operations

Example project directory:

/home/student/project/
├── src
├── config
└── logs

If inside project:

cd src

is easier than:

cd /home/student/project/src

9. Common Commands That Use Paths

Many Linux commands require paths.

Examples:

CommandPurpose
cdChange directory
lsList directory contents
cpCopy files
mvMove files
rmDelete files
catDisplay file contents
nano / vimEdit files

Example:

cp /home/student/file.txt /home/student/backup/

or using relative paths:

cp file.txt backup/

10. Exam Tips for LPI Linux Essentials

For the LPI 010-160 exam, remember these key points:

Absolute path

  • Starts with /
  • Always shows full location

Relative path

  • Based on current directory
  • Does not start with /

✔ Special symbols

SymbolMeaning
.Current directory
..Parent directory

✔ Use pwd to check the current directory.

✔ Commands like cd, ls, cp, mv, and rm can use both absolute and relative paths.


11. Quick Summary

  • A path shows where a file or directory is located in the Linux filesystem.
  • Absolute paths start from the root directory / and provide the full location.
  • Relative paths start from the current working directory.
  • Special symbols help with navigation:
    • . → current directory
    • .. → parent directory
  • Linux commands use paths to access files and directories.

Understanding absolute and relative paths is essential for navigating the Linux filesystem and performing file operations, which is a key skill tested in the Linux Essentials certification exam.

Buy Me a Coffee