Lesson 2.2: Basic Commands
Duration: 45 minutes
Learning Objectives
After completing this lesson, you will be able to:
- Navigate the file system using
cdandpwd - List directory contents with
lsand its useful options - Create directories with
mkdir - Understand absolute vs relative paths in the terminal
Introduction
Now that you understand why the terminal is powerful, let's learn the commands that will become second nature to you. In this lesson, we focus on navigation and creating directories — the foundation of everything you'll do in the terminal.
Think of these commands as learning to walk before you run. Once you master cd, ls, pwd, and mkdir, everything else becomes easier.
Main Content
The pwd Command: Where Am I?
pwd stands for Print Working Directory. It shows your current location in the file system.
$ pwd
/Users/alice/Documents
This tells you exactly where you are. The path shows each folder from the root (/) to your current location.
Why it matters: Before doing anything, it's good practice to check where you are. Many mistakes happen because you're in the wrong directory.
The ls Command: What's Here?
ls lists the contents of a directory.
$ ls
Desktop Documents Downloads Music Pictures Videos
Useful ls Options
| Option | What It Does | Example |
|---|---|---|
-l |
Long format (details) | ls -l |
-a |
Show all (including hidden) | ls -a |
-h |
Human-readable sizes | ls -lh |
-t |
Sort by modification time | ls -lt |
-r |
Reverse order | ls -lr |
Combine options:
$ ls -la
total 48
drwxr-xr-x 12 alice staff 384 Jan 8 10:30 .
drwxr-xr-x 5 alice staff 160 Jan 8 09:15 ..
-rw-r--r-- 1 alice staff 1024 Jan 8 10:30 .hidden-file
drwxr-xr-x 3 alice staff 96 Jan 8 10:00 Documents
-rw-r--r-- 1 alice staff 2048 Jan 8 09:45 notes.txt
Reading the long format:
Listing a Specific Directory
You can list contents of any directory without going there:
$ ls Documents
projects notes.txt resume.pdf
$ ls /Users/alice/Downloads
file1.zip image.png setup.exe
The cd Command: Let's Go Somewhere
cd stands for Change Directory. This is how you navigate.
$ pwd
/Users/alice
$ cd Documents
$ pwd
/Users/alice/Documents
Special Directory Symbols
| Symbol | Meaning | Example |
|---|---|---|
. |
Current directory | cd . (stays in place) |
.. |
Parent directory (one level up) | cd .. |
~ |
Home directory | cd ~ |
- |
Previous directory | cd - |
/ |
Root directory | cd / |
Examples:
# Go to home directory
$ cd ~
# Go up one level
$ cd ..
# Go up two levels
$ cd ../..
# Go to root
$ cd /
# Go back to where you just were
$ cd -
Navigating with Paths
Absolute paths start from the root (/):
$ cd /Users/alice/Documents/projects
Relative paths start from your current location:
$ pwd
/Users/alice
$ cd Documents/projects # Relative to current location
Visual representation:
If you're at /Users/alice:
- Absolute path: /Users/alice/Documents/projects
- Relative path: Documents/projects
If you're at /Users/alice/Downloads:
- Absolute path: /Users/alice/Documents/projects
- Relative path: ../Documents/projects
Tab Completion (Your Best Friend)
Instead of typing full names, press Tab to autocomplete:
$ cd Doc[Tab]
$ cd Documents/ # Autocompleted!
$ cd Documents/pro[Tab]
$ cd Documents/projects/ # Autocompleted!
If there are multiple matches, press Tab twice to see options:
$ cd D[Tab][Tab]
Desktop/ Documents/ Downloads/
This saves time and prevents typos.
The mkdir Command: Creating Directories
mkdir makes a new directory.
$ mkdir projects
$ ls
Documents Downloads projects # New folder created!
Creating Multiple Directories
$ mkdir folder1 folder2 folder3
$ ls
folder1 folder2 folder3
Creating Nested Directories
By default, mkdir can't create parent directories that don't exist:
$ mkdir projects/web/frontend
mkdir: projects/web: No such file or directory # Error!
Use the -p flag to create parent directories automatically:
$ mkdir -p projects/web/frontend
$ ls projects
web
$ ls projects/web
frontend
The -p flag is safe — it won't error if directories already exist.
Naming Conventions
| Do | Don't |
|---|---|
my-project |
my project (spaces cause problems) |
myProject |
My Project! (special characters cause problems) |
my_project |
my/project (slash is a path separator) |
If you must use spaces, escape them or use quotes:
$ mkdir "my project" # Quotes
$ mkdir my\ project # Escape with backslash
Putting It All Together
Let's practice a common workflow:
# 1. Check where you are
$ pwd
/Users/alice
# 2. Go to Documents
$ cd Documents
# 3. See what's there
$ ls
notes.txt resume.pdf
# 4. Create a new project folder structure
$ mkdir -p coding/typescript-course/exercises
# 5. Navigate into it
$ cd coding/typescript-course/exercises
# 6. Verify your location
$ pwd
/Users/alice/Documents/coding/typescript-course/exercises
# 7. Go back home
$ cd ~
# 8. Return to where you were
$ cd -
/Users/alice/Documents/coding/typescript-course/exercises
Practice Exercise
Task
Complete these navigation challenges in your terminal:
Challenge 1: Navigation
- Open your terminal
- Check your current directory with
pwd - Navigate to your home directory with
cd ~ - List all contents including hidden files with
ls -la - Count how many hidden files/folders you see (items starting with
.)
Challenge 2: Creating Structure
- Navigate to your Documents folder
- Create this structure using a single command:
Documents/ └── terminal-practice/ ├── lesson-1/ ├── lesson-2/ └── lesson-3/ - Navigate into
lesson-2using a relative path - Verify your location with
pwd - Go back to Documents using
..
Challenge 3: Speed Run
- Starting from anywhere, get to your home directory
- Create a folder called
test-navigation - Go into it
- Go back out
- Create another folder called
test-navigation-2 - Using one command, navigate to
test-navigationfromtest-navigation-2
Expected Commands
Challenge 1 Solution
pwd
cd ~
ls -la
# Count items starting with . (hidden files)
Challenge 2 Solution
cd ~/Documents
mkdir -p terminal-practice/lesson-1 terminal-practice/lesson-2 terminal-practice/lesson-3
# OR
mkdir -p terminal-practice/{lesson-1,lesson-2,lesson-3}
cd terminal-practice/lesson-2
pwd
cd ../..
Challenge 3 Solution
cd ~
mkdir test-navigation
cd test-navigation
cd ..
mkdir test-navigation-2
cd test-navigation-2
cd ../test-navigation
Key Takeaways
pwdshows your current location — use it oftenlsshows directory contents; add-lafor full details including hidden filescdmoves you around; remember~(home),..(up),-(previous)mkdircreates directories; use-pfor nested structures- Tab completion saves time and prevents typos — use it constantly
- Prefer relative paths for nearby locations, absolute paths for precision
- Avoid spaces in folder names; use hyphens or underscores instead
Resources
| Resource | Type | Difficulty |
|---|---|---|
| Linux Navigation - LinuxCommand.org | Tutorial | Beginner |
| Bash Navigation Cheat Sheet - DevHints | Reference | Beginner |
| SS64 Command Line Reference | Reference | Beginner |