Skip to main content

🗂️ Lesson 2.2: Navigating the Filesystem

Everything in Linux is a file. Let's learn how they're organized and how to move around.

🎯 Learning Objectives

  • Understand the Linux directory hierarchy
  • Know the purpose of key system directories
  • Navigate using cd, pwd, and ls
  • Distinguish between absolute and relative paths
  • Use shortcuts like ~, ., .., and -

Estimated Time: 40 minutes

📑 In This Lesson

The Directory Tree

Linux organizes everything in a single upside-down tree. At the very top is / — the root directory (not to be confused with the root user). Every file and folder on the system branches off from here.

graph TD ROOT["/ (root)"] --> BIN["/bin"] ROOT --> ETC["/etc"] ROOT --> HOME["/home"] ROOT --> VAR["/var"] ROOT --> TMP["/tmp"] ROOT --> USR["/usr"] ROOT --> OPT["/opt"] ROOT --> MNT["/mnt"] HOME --> USER1["/home/ray"] HOME --> USER2["/home/alice"] USER1 --> DESK["Desktop"] USER1 --> DOCS["Documents"] USER1 --> DL["Downloads"] USR --> USRBIN["/usr/bin"] USR --> USRLIB["/usr/lib"] style ROOT fill:#3b82f6,stroke:#1e40af,color:#fff style HOME fill:#22c55e,stroke:#166534,color:#fff style USER1 fill:#6366f1,stroke:#4338ca,color:#fff

💡 Everything Is a File

Linux treats nearly everything as a file — regular files, directories, USB drives, even your keyboard and screen are represented as files. This is one of Linux's core design principles and it makes the system remarkably consistent.

🐧 Windows vs. macOS vs. Linux Paths

Windows uses backslashes and drive letters: C:\Users\ray\Documents. Linux uses forward slashes and a single root: /home/ray/Documents. macOS also uses forward slashes, but home directories live under /Users/: /Users/ray/Documents. In Linux, there are no drive letters — external drives are "mounted" into the tree (e.g., at /mnt/usb). On macOS, external drives mount under /Volumes/.

Key Directories Explained

You don't need to memorize all of these — just know they exist so you can find things when you need to:

Directory Purpose You'll Use It?
/ Root of the entire filesystem Rarely directly
/home User home directories ⭐ All the time
/etc System configuration files When configuring software
/var Variable data: logs, caches, databases When checking logs
/tmp Temporary files (cleared on reboot) For scratch work
/bin Essential command binaries Where ls, cp, etc. live
/usr User programs, libraries, docs Where most software is installed
/opt Optional / third-party software Some apps install here
/mnt Mount point for external drives WSL mounts Windows drives here
/dev Device files (disks, USB, etc.) Rarely — it's for the kernel
/proc Virtual filesystem for process info Advanced troubleshooting

✅ Your Home Is Your Castle

As a regular user, you'll spend 99% of your time in /home/yourname (or just ~). This is where your files, projects, configuration, and downloads live. You can read system directories, but you need sudo to modify them.

Moving Around: pwd and cd

Two commands are all you need to navigate:

pwd — Print Working Directory

Shows you where you are right now:

pwd
# Output: /home/ray

cd — Change Directory

Moves you to a different directory:

# Go to your Documents folder
cd Documents

# Check where you are now
pwd
# Output: /home/ray/Documents

# Go up one level (back to /home/ray)
cd ..

# Go to the root of the filesystem
cd /

# Go straight home from anywhere
cd
# or
cd ~

💡 cd With No Arguments

Running cd by itself (no path) always takes you home. It's the same as cd ~. This works no matter where you are in the filesystem.

Absolute vs. Relative Paths

There are two ways to specify a location:

Type Starts With Meaning Example
Absolute / Full path from the root /home/ray/Documents
Relative Anything else Path from your current location Documents or ./Documents

Think of it like giving directions:

  • Absolute: "Go to 123 Main Street, Springfield" — works from anywhere
  • Relative: "Turn left and go two blocks" — depends on where you're standing
# These are equivalent when you're in /home/ray:
cd /home/ray/Documents     # Absolute — works from anywhere
cd Documents               # Relative — only works from /home/ray

# These are also equivalent:
ls /var/log                # Absolute
cd /var && ls log          # Navigate then list with relative path

⚠️ When to Use Which

Use absolute paths in scripts and config files (they always work). Use relative paths when typing interactively (less typing). If you're ever confused about where a relative path points, run pwd first.

Path Shortcuts

Bash provides several shortcuts that save you from typing long paths:

Shortcut Meaning Example
~ Your home directory cd ~/home/ray
. Current directory ./script.sh → run a script here
.. Parent directory (one level up) cd .. → go up one level
- Previous directory cd - → go back to where you were
# Chain .. to go up multiple levels
cd ../..          # Up two levels

# Combine shortcuts with paths
cd ~/Documents    # Go to Documents in your home folder
ls ../Downloads   # List the Downloads folder next to your current one

# Bounce between two directories
cd /var/log
cd /etc
cd -              # Jumps back to /var/log
cd -              # Jumps back to /etc

✅ The cd - Bounce

The cd - shortcut is incredibly useful when you're working in two directories at once. It's like an "undo" for navigation — it always takes you back to the last place you were.

Listing Files in Detail

You've already met ls, but it has many useful options:

# Basic listing
ls

# One file per line
ls -1

# Long format (permissions, owner, size, date)
ls -l

# Include hidden files (start with a dot)
ls -a

# Long format + hidden files (most common combo)
ls -la

# Human-readable sizes (KB, MB, GB instead of bytes)
ls -lh

# Sort by modification time (newest first)
ls -lt

# Sort by size (largest first)
ls -lS

# Reverse any sort order
ls -ltr    # oldest first

Reading ls -l Output

Let's decode a typical line:

-rw-r--r-- 1 ray ray 4096 Apr 14 10:30 notes.txt
Part Value Meaning
Type + Permissions -rw-r--r-- Regular file; owner can read/write, others can only read
Links 1 Number of hard links
Owner ray The user who owns the file
Group ray The group that owns the file
Size 4096 File size in bytes
Date Apr 14 10:30 Last modified date/time
Name notes.txt The filename

💡 Hidden Files

Files and directories whose names start with a dot (.) are hidden by default. They won't show up with plain ls — you need ls -a. Linux uses hidden files for configuration: .bashrc, .profile, .config/, etc. Don't delete these unless you know what they do!

Visualizing with tree

The tree command shows a visual representation of a directory structure:

# Install tree (it may not be installed by default)
sudo apt install tree -y

# Show the current directory as a tree
tree

# Limit depth to 2 levels
tree -L 2

# Show hidden files too
tree -a

# Show only directories
tree -d

# Show a specific folder
tree ~/Documents -L 2

Example Output:

/home/ray
├── Desktop
├── Documents
│   ├── notes.txt
│   └── projects
│       ├── website
│       └── scripts
├── Downloads
│   └── ubuntu-24.04.iso
├── .bashrc
└── .profile

5 directories, 4 files

🐧 Other Distros

On Fedora, use sudo dnf install tree. On Arch, use sudo pacman -S tree. The command works the same once installed.

Exercises

🏋️ Exercise 1: Navigate the Tree

Starting from your home directory, complete this sequence:

# 1. Confirm you're home
pwd

# 2. Go to the root
cd /

# 3. List what's here
ls

# 4. Go into /var/log
cd var/log

# 5. Where are you now?
pwd

# 6. Go home with one command
cd ~

# 7. Confirm
pwd
💡 Expected Output

Step 2: /. Step 4: /var/log. Step 6: /home/yourname.

🏋️ Exercise 2: Absolute vs. Relative

Do these commands produce the same output? Try each one:

# From your home directory:
ls Documents
ls ~/Documents
ls /home/$USER/Documents
💡 Solution

Yes — all three list the same directory. The first is relative, the second uses the ~ shortcut, and the third is fully absolute (with $USER expanding to your username).

🏋️ Exercise 3: The cd - Bounce

  1. cd /etc
  2. cd /var/log
  3. cd - — where are you?
  4. cd - again — and now?
💡 Solution

Step 3: /etc (it bounced back). Step 4: /var/log (bounced again). You can keep bouncing between the two.

🏋️ Exercise 4: Explore System Directories

Use ls to look inside these directories. What kind of things do you find?

ls /bin | head -20       # First 20 commands
ls /etc | head -20       # First 20 config files
ls /var/log              # System logs
ls /tmp                  # Temporary files
💡 What You'll See
  • /bin — familiar commands like ls, cp, mv, cat
  • /etc — configuration files like hosts, passwd, apt/
  • /var/log — log files like syslog, auth.log
  • /tmp — random temporary files (or empty)

🏋️ Exercise 5: Install and Use tree

# Install tree
sudo apt install tree -y

# View your home directory, 2 levels deep
tree ~ -L 2

# View only directories
tree ~ -d -L 2

Knowledge Check

❓ Question 1

Which directory is at the very top of the Linux filesystem hierarchy?

❓ Question 2

If you're in /home/ray/Documents and run cd .., where do you end up?

❓ Question 3

Which path is absolute?

Summary

🎉 Key Takeaways

  • Linux uses a single directory tree rooted at /
  • Your home directory (~ or /home/yourname) is where you do most of your work
  • pwd shows where you are; cd moves you somewhere else
  • Absolute paths start with / and work from anywhere; relative paths are based on your current location
  • Shortcuts: ~ (home), .. (parent), . (current), - (previous)
  • ls -la shows all files with details; tree shows a visual directory map

🚀 What's Next?

You can navigate the filesystem — now it's time to start creating, copying, moving, and deleting files and directories.