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 cd Documents to move in, then ls to see files. Use cd .. to go back. Check your location with pwd.
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/
Explore the virtual file system with cd and ls. Try cd Documents -> ls -la -> cd .. -> cd ~ and so on.
"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: ownerstaff: group96,1200: file size in bytesJan 15 10:30: last modified timeDocuments,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
- Run
cd ~to go home, thenpwdto confirm your location. - Run
ls -lato see the full listing including hidden files. Count how many files start with.. - Move into
cd Documents, then return withcd ... - Move into
cd Documentsagain, then trycd -to go back. Compare the result withcd ... - Run
ls -lh ~to see file sizes in a human-readable format.
Explore the virtual file system with cd and ls. Try cd Documents, ls -la, cd .., cd ~ and more.
You're at /Users/dale/Documents/project. What's the shortest command to get home?