Kahibaro
Discord Login Register

Absolute vs relative paths

Understanding Absolute vs Relative Paths

When working with files and directories, you constantly refer to paths. In Linux, these paths come in two main forms:

This chapter focuses on how these two types of paths work, how to recognize them, and how to use them correctly.

Absolute Paths

An absolute path always starts from the root directory / and describes the full route to a file or directory.

Key characteristics:

Examples:

You can think of an absolute path as a full street address including country, city, and house number: it uniquely identifies a location on the system.

When to use absolute paths

Absolute paths are useful when:

Relative Paths

A relative path is interpreted starting from your current directory (often called the current working directory).

Key characteristics:

Examples (assume pwd shows /home/alex):

If you move to a different directory, the same relative path may point somewhere else or not exist at all.

Special entries: `.` and `..`

Two special directory names are essential for relative paths:

They are most useful inside relative paths:

These are always relative; you will not see valid absolute paths like /../home.

Recognizing Absolute vs Relative Quickly

You can identify the type of path just by looking at the first character:

Examples — classify each:

Note: tools will internally simplify paths like /etc/../var/log to /var/log.

How the Shell Interprets Paths

The shell (like bash) interprets paths roughly like this:

You can always see the full absolute path of your current directory using:

pwd

From that, you can mentally expand a relative path into its absolute form by “appending” it to the output of pwd and simplifying any . or ...

Example:

Common Uses and Differences in Practice

Using `cd` with absolute vs relative paths

Using commands that accept paths

Commands like ls, cp, mv, rm, cat, etc., all accept both absolute and relative paths:

The command doesn’t care which form you use; you must understand from where a relative path is evaluated.

Converting Between Absolute and Relative Paths (Conceptually)

You often need to “convert” between path types in your head:

From absolute to relative

Given:

You can move up to the common ancestor (/home/alex/projects) and then down:

So a relative path from webapp to that file is:

From relative to absolute

Given:

Combine and simplify:

Choosing Which Type to Use

Guidelines:

In many real-world situations, you mix both: an absolute path for a destination and a relative path for a source, or vice versa.

Practice Ideas

To become comfortable with absolute and relative paths, try:

  1. Use pwd to see where you are.
  2. From your home directory:
    • Use cd with an absolute path to go to /var/log.
    • Go back using cd and a relative path only (.. etc.).
  3. Inside a nested project directory (e.g. ~/project/src/module), figure out:
    • The absolute path to a file in ../docs
    • A relative path from docs back to src/module

Regular practice like this will make reading and writing both types of paths feel natural.

Views: 22

Comments

Please login to add a comment.

Don't have an account? Register now!