DaleSchool

Directory Navigation

Beginner20min

Learning Objectives

  • Describe the file system's tree structure
  • Explain the difference between absolute and relative paths
  • Navigate to any directory using cd
  • View file listings with various ls options

Working Code

Example 1: Check your location and move around

pwd

Example output:

/Users/dale

pwd stands for Print Working Directory. It tells you where you are in the file system.

Move into a subdirectory:

cd Documents
pwd

Output:

/Users/dale/Documents

Go up one level:

cd ..
pwd

Output:

/Users/dale

Example 2: Jump straight home

Get back home from anywhere:

cd /tmp
pwd
cd ~
pwd

Output:

/tmp
/Users/dale

~ always refers to your home directory. No need to memorize long paths like /Users/dale.

Example 3: Detailed file listings with ls

ls

Output:

Desktop  Documents  Downloads  Movies  Music  Pictures

With detailed info:

ls -l

Output:

drwx------  5 dale staff   160 Jan 15 10:30 Desktop
drwx------ 10 dale staff   320 Jan 14 09:00 Documents
drwx------  3 dale staff    96 Jan 13 15:20 Downloads

Including hidden files:

ls -la

Output (files starting with . are hidden):

drwxr-xr-x  20 dale staff   640 Jan 15 10:30 .
drwxr-xr-x   8 root admin   256 Jan  1 00:00 ..
-rw-r--r--   1 dale staff  3771 Jan  1 00:00 .zshrc
drwx------   5 dale staff   160 Jan 15 10:30 Desktop
drwx------ 10 dale staff   320 Jan 14 09:00 Documents

Try It Yourself

Practice different ways to move around:

# Jump home (from anywhere)
cd ~

# Go back to the previous directory (very useful!)
cd Documents
cd -
# -> returns to the directory you were just in

# Move using an absolute path (same result from anywhere)
cd /tmp

# Multiple levels at once
cd ~/Documents

# Go up multiple levels
cd ../..

Try various ls options:

# Basic listing
ls

# Include hidden files
ls -a

# Detailed info (permissions, size, date)
ls -l

# Combine both (most commonly used)
ls -la

# Human-readable file sizes (K, M, G)
ls -lh

# View a specific directory
ls -la Documents/

"Why?" — Understanding the File System Tree

The file system is a tree structure. The top is / (root), and all directories and files branch out from there.

/                          <- root (top level)
├── Users/
│   ├── dale/              <- ~ (my home directory)
│   │   ├── Desktop/
│   │   ├── Documents/
│   │   │   ├── report.pdf
│   │   │   └── notes.txt
│   │   └── Downloads/
│   └── guest/
├── tmp/                   <- temporary files
├── usr/
│   └── bin/               <- system commands live here
└── etc/                   <- system configuration files

Understanding this structure lets you navigate to any location.

Absolute vs. Relative Paths

| Type | Example | Description | | -------- | ---------------------------- | ----------------------------------- | | Absolute | /Users/dale/Documents | Starts from /, same from anywhere | | Relative | Documents or ./Documents | Relative to current location |

When to use which?

  • Absolute paths: in scripts, when clarity matters
  • Relative paths: for interactive use, accessing nearby files

Special Path Symbols

| Symbol | Meaning | Example | | ------ | ------------------ | ------------------------ | | ~ | Home directory | cd ~, ls ~/Documents | | . | Current directory | ls ., ./script.sh | | .. | Parent directory | cd .., ls ../sibling | | - | Previous directory | cd - |

Common Mistakes

Mistake 1: Wrong path separator

# Correct (Mac/Linux)
cd Documents/notes

# Wrong (Windows style)
cd Documents\notes

Mac and Linux use / (forward slash), Windows uses \ (backslash).

Mistake 2: Navigating to a nonexistent directory

cd documants   # typo!
cd: no such file or directory: documants

Press Tab after typing cd to auto-complete. It's the best way to avoid typos.

Mistake 3: Directory names with spaces

# Error: tries to process "My" and "Documents" separately
cd My Documents

# Correct: wrap in quotes or escape with backslash
cd "My Documents"
cd My\ Documents

Deep Dive

Reading ls -la output
drwxr-xr-x  3 dale staff   96 Jan 15 10:30 Documents
-rw-r--r--  1 dale staff 1200 Jan 14 09:00 README.md
  • First character: d=directory, -=file, l=symbolic link
  • rwxr-xr-x: owner/group/others permissions (r=read, w=write, x=execute)
  • Number 3, 1: hard link count
  • dale: owner
  • staff: group
  • 96, 1200: file size in bytes
  • Jan 15 10:30: last modified time
  • Documents, README.md: name

The permission system is covered in detail in Lesson 11.

Essential Tab completion

Press Tab while typing a path to auto-complete:

cd Doc[Tab]  ->  cd Documents/

If multiple matches exist, press Tab twice to see the list:

cd D[Tab][Tab]
# Desktop/  Documents/  Downloads/

Never type full paths manually. Using Tab aggressively reduces mistakes and speeds you up.

Visualize directory structure with tree

Install the tree command to see directory structures as a tree:

# macOS
brew install tree

# Usage
tree Documents/

Output:

Documents/
├── project/
│   ├── README.md
│   └── src/
│       └── main.py
└── notes.txt

To limit depth:

tree -L 2  # up to 2 levels
  1. Run cd ~ to go home, then pwd to confirm your location.
  2. Run ls -la to see the full listing including hidden files. Count how many files start with ..
  3. Move into cd Documents, then return with cd ...
  4. Move into cd Documents again, then try cd - to go back. Compare the result with cd ...
  5. Run ls -lh ~ to see file sizes in a human-readable format.

Q1. You're at /Users/dale/Documents/project. What's the shortest command to get home?

  • A) cd /Users/dale
  • B) cd ~
  • C) cd ../..
  • D) cd /