Sagar.BlogArticle
All posts
All posts
Linux

cd — Navigate the Linux Filesystem

Master the cd command: absolute paths, relative paths, home shortcuts, the parent directory, and the cd - trick that saves you every day.

January 7, 20255 min read
LinuxNavigationTerminal

cd (Change Directory) is the command you'll type more than almost any other. It moves you through the filesystem tree. Like walking between rooms — except you can teleport to any room instantly by knowing its full address.

Syntax

cd [DIRECTORY]

# cd is a shell builtin — use "help cd" not "man cd"
help cd

Special Shortcuts

cd Special Arguments

ShortcutDescriptionExpands To
cdGo home (no argument)cd /home/sagar
cd ~Go home (explicit)cd /home/sagar
cd ..Go up one levelParent directory
cd ../..Go up two levelsGrandparent directory
cd -Toggle to previous directoryLast place you were
cd .Stay in place (no-op)Current directory

Absolute vs Relative Navigation

cd-examples.sh
# ─── Absolute paths (start with /) ──────────────────────────
cd /home/sagar/Documents     # Full path from root
cd /etc                      # System config directory
cd /var/log                  # Log files
cd /                         # The root itself

# ─── Relative paths (from where you currently are) ───────────
# Assuming you're in /home/sagar:
cd Documents                 # → /home/sagar/Documents
cd Documents/work            # → /home/sagar/Documents/work

# Assuming you're in /home/sagar/Documents:
cd ..                        # → /home/sagar
cd ../Downloads              # → /home/sagar/Downloads
cd ../../etc                 # → /etc (up 2, then into etc)

# ─── Home directory ──────────────────────────────────────────
cd                           # Go home — fastest way!
cd ~                         # Same thing, explicit ~
cd ~/Projects/myapp          # Go to a project folder

# ─── Back and forth ──────────────────────────────────────────
cd /var/log
cd /etc/nginx
cd -                         # → /var/log
cd -                         # → /etc/nginx  (keeps toggling!)

Real-World Patterns

These are the cd patterns you'll use every day in real work:

# Pattern 1: Go to project, work, return to where you were
cd ~/Projects/backend
# ... edit files, run tests ...
cd -                         # Back to previous directory instantly

# Pattern 2: Navigate to sibling directory
# Currently in: /home/sagar/Documents
cd ../Downloads              # Sibling — go up then sideways

# Pattern 3: Deep jump, then home
cd /var/lib/docker/volumes/abc123/_data
# deep work...
cd                           # Instant jump home, don't type the path

# Pattern 4: Tab completion (essential!)
cd /ho → Tab → /home/
cd /home/sa → Tab → /home/sagar/
cd /home/sagar/Pro → Tab → /home/sagar/Projects/

cd Has No man Page

cd is a shell builtin — it's built into bash/zsh itself, not an external program. That's why man cd typically opens a generic page or fails. Use help cd (bash) or run-help cd (zsh) to see the real documentation.


Quick Check

You're in `/home/sagar/Projects/backend/src`. Which command takes you to `/home/sagar/Projects/frontend`?

Quick Check

What does `cd -` do?

Exercise

Navigation drill — complete each step without using the up arrow:

  1. Go to /var/log
  2. Without typing the full path, go to the sibling /var/cache
  3. Jump back to /var/log using one keystroke/command
  4. Go three levels deep into your home directory (create folders if needed)
  5. Return home with a single command