Practical Unix
Lesson 2 of 2100%
Lesson 2·20 min

Navigating the Filesystem

What you'll learn

  • Understand the Unix filesystem hierarchy
  • Navigate between directories with cd
  • Use absolute and relative paths
  • Master shortcuts like ~ and ..

The Filesystem Tree

Unix organizes everything in a tree structure starting from the root directory /. Everything - files, directories, devices, even running processes - lives somewhere in this tree.

/
├── home/          # User home directories
│   └── swarup/    # Your stuff lives here
├── etc/           # System configuration
├── var/           # Variable data (logs, databases)
├── usr/           # User programs and utilities
├── tmp/           # Temporary files
└── bin/           # Essential command binaries

Note

Unlike Windows with its `C:\`, `D:\` drive letters, Unix has a single unified tree. External drives get "mounted" into this tree as directories.

Where Am I? (pwd)

The pwd command (print working directory) tells you your current location:

pwd

You'll see something like /home/swarup or /Users/swarup on macOS.

Moving Around (cd)

The cd command (change directory) moves you around:

cd /var/log
pwd

You're now in the system logs directory.

Absolute vs Relative Paths

Absolute paths start from root (/):

cd /home/swarup/projects

Relative paths start from your current location:

cd projects/website

If you're in /home/swarup, this takes you to /home/swarup/projects/website.

Essential Shortcuts

These shortcuts will save you thousands of keystrokes:

ShortcutMeaning
~Your home directory
.Current directory
..Parent directory
-Previous directory

Examples

cd ~

Go home, no matter where you are.

cd ..

Go up one level.

cd ../..

Go up two levels.

cd -

Toggle between current and previous directory. Super useful!

Tip

Combine these! `cd ~/projects` takes you to the projects folder in your home directory from anywhere.

Listing Contents (ls)

We saw ls briefly. Let's explore it more:

ls

Basic listing.

ls -l

Long format with details.

ls -la

Long format including hidden files (those starting with .).

ls -lh

Long format with human-readable sizes (KB, MB, GB instead of bytes).

Understanding ls -l Output

drwxr-xr-x  5 swarup staff  160 Dec 14 10:30 projects
-rw-r--r--  1 swarup staff 1234 Dec 14 09:15 notes.txt
PartMeaning
d or -Directory or file
rwxr-xr-xPermissions (we'll cover this later)
5 or 1Number of links
swarupOwner
staffGroup
160Size in bytes
Dec 14 10:30Last modified
projectsName

Tab Completion

This is a game-changer. Start typing a path and press Tab:

cd ~/Doc<Tab>

The shell completes it to ~/Documents/. If there are multiple matches, press Tab twice to see options.

Warning

Tab completion is your friend. If it doesn't complete, either the path doesn't exist or you need to type more characters to disambiguate.

Practice Exercise

  1. Navigate to your home directory
  2. Create a mental map: run ls -la and identify hidden files (hint: they start with .)
  3. Navigate to /var/log and back home using cd -
  4. Try tab-completing a long path

Key Takeaways

  • The filesystem is a tree starting at /
  • pwd shows where you are, cd moves you around
  • Use ~, .., and - to navigate efficiently
  • Tab completion saves time and prevents typos
  • ls -la is your go-to for seeing what's in a directory

Next up: creating, copying, and moving files.