linux navigation

LINUX NAVIGATION (Absolute Path Vs Relative Path)

In this post, I’ll walk you through a simple “playground” setup where we’ll create directories and files safely, then practice moving around using both absolute and relative paths. Along the way, you’ll see why the -P option is a lifesaver when building nested folders, and how commands like cd, ls, cp and rm behave depending on the path you give them.

Absolute Paths

An absolute path specifies the complete location of a file or directory starting from the root directory (/). It always begins with a forward slash.

Examples is: /home/olalekan/documents/file1.txt

No matter where you currently are in the filesystem, an absolute path always points to the same location.

Relative Paths

A relative path specifies the location of a file or directory relative to your current working directory. It does NOT start with a forward slash.

Example is: documents/file.txt (file in a subdirectory)

Absolute and relative navigation in practice,

Part 1: The Setup (Do this first)

Open your terminal. Run these commands to create a safe “playground” directory structure for this assignment. This prevents you from messing up your actual files.

The -p flag stands for “parents”.

Here is why it is critical in that specific command:

  1. Without -p:If you ran mkdir ~/assignment/music/rock, Linux would try to create the final folder rock. However, if the folders assignment or music didn’t exist yet, the command would fail with an error like:

mkdir: cannot create directory ‘rock’: No such file or directory

image
  1. With -p:It tells Linux: “Please create the final directory rock, and also create any missing parent directories (assignment and music) along the way.”

mkdir -p ~/assignment/music/rock
mkdir -p ~/assignment/music/jazz
mkdir -p ~/assignment/photos/2023
mkdir -p ~/assignment/photos/2024
touch ~/assignment/music/rock/song1.mp3
touch ~/assignment/photos/2023/photo1.jpg

image

Part 2: The Assignment Task

Task 1: Absolute Navigation

  1. No matter where you are right now, write the command to change directory (cd) into the rock folder using the Absolute Path.
  2. List (ls) the contents of the /var/log directory using the Absolute Path (do not move into that directory, just list it).
image

Task 2: Relative Navigation Assume you are currently inside the ~/assignment/music/rock directory for these questions.3.  Move up one level to the music directory using a Relative Path. 4.  Move straight to the jazz directory (which is a sibling of rock) using a Relative Path. 5.  Move all the way to ~/assignment/photos/2023 using a Relative Path.

image

Task 3: File Operations Assume you are currently inside ~/assignment (the root of our playground).6.  Copy (cp) the file photo1.jpg (which is inside photos/2023) into your current directory using a Relative Path for the source. 7.  Remove (rm) the file song1.mp3 using the Absolute Path.

image

These are the building blocks that prepare you for more advanced commands and scripting down the road. Keep experimenting, break things in your “playground” setup, and rebuild them again.

1 thought on “LINUX NAVIGATION (Absolute Path Vs Relative Path)”

Leave a Comment

Your email address will not be published. Required fields are marked *