Chapter 7: Bash Change Dir (cd)
Bash Change Dir (cd) — the cd command, which stands for “Change Directory”.
This is one of the first three commands every beginner learns (along with pwd and ls), because without cd, you’re stuck in one place forever in the terminal!
Think of your computer’s file system like a big multi-story building in Hyderabad:
- Root (/) = the ground floor / basement (everything starts here)
- Your home (/home/webliance) = your personal flat on the 5th floor
- Folders like Documents, Downloads, Projects = rooms inside your flat
- cd = the elevator / stairs you use to move between floors and rooms
Without cd, you can’t visit other rooms or floors!
Basic Syntax
|
0 1 2 3 4 5 6 |
cd [directory] |
- No argument → go to your home directory
- With argument → go to that place
Most Common & Useful Ways to Use cd (Practice These Right Now!)
- Go Home (your safest, most important place)
|
0 1 2 3 4 5 6 7 8 9 10 |
cd # or cd ~ # or cd $HOME |
All three do the exact same thing — take you to /home/webliance (or /Users/webliance on macOS).
After typing any of them:
|
0 1 2 3 4 5 6 |
pwd |
→ You should see something like /home/webliance
- Go up one level (parent directory)
|
0 1 2 3 4 5 6 |
cd .. |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
cd ~/Documents/my_project pwd # → /home/webliance/Documents/my_project cd .. pwd # → /home/webliance/Documents cd .. # again pwd # → /home/webliance |
.. = special name for “parent folder”
- Go to root (top of the entire system)
|
0 1 2 3 4 5 6 |
cd / |
→ Now you’re at the very top — be careful here, don’t delete anything!
- Go back to the previous directory (super useful!)
|
0 1 2 3 4 5 6 |
cd - |
This jumps to where you were before the last cd.
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
cd /etc pwd # → /etc cd ~ pwd # → /home/webliance cd - pwd # → /etc (back again!) |
Bash remembers the previous location in a variable called $OLDPWD.
→ cd – also prints the directory it goes to (very helpful).
- Go to a specific folder (relative or absolute path)
Relative (from where you are now):
|
0 1 2 3 4 5 6 7 8 |
cd Documents cd Downloads/photos cd ../backup |
Absolute (full path from root):
|
0 1 2 3 4 5 6 7 |
cd /var/log cd /home/webliance/projects/myapp/src |
- Multiple levels at once
|
0 1 2 3 4 5 6 |
cd ~/projects/webliance/2026/bash_tutorial/session5 |
Bash creates the path in your mind — no need to cd step-by-step.
Special Tilde (~) Tricks (Bash Magic!)
Tilde ~ is a shortcut for your home.
- ~ = your home
- ~/ = same as above (with slash)
- ~username = someone else’s home (if you have permission)
Examples:
|
0 1 2 3 4 5 6 7 8 |
cd ~friend # → /home/friend (if exists) cd ~/Desktop ls ~root # → shows /root contents (usually needs sudo) |
Advanced tilde (rare but cool):
- ~+ = current directory (same as $PWD)
- ~- = previous directory (same as $OLDPWD)
|
0 1 2 3 4 5 6 7 |
echo ~+ # → /home/webliance/current/place cd ~- |
Quick Reference Table (Memorize This!)
| What you want to do | Command | Example Output / Result |
|---|---|---|
| Go to your home | cd or cd ~ | /home/webliance |
| Go up one folder | cd .. | From Documents → home |
| Go up two folders | cd ../.. | From src → project root |
| Go back to previous location | cd – | Prints and jumps back |
| Go to root | cd / | / |
| Go to specific folder (relative) | cd foldername | cd Pictures |
| Go deep relative | cd folder/subfolder | cd Downloads/wallpapers |
| Full absolute path | cd /path/from/root | cd /etc/systemd |
| Someone else’s home | cd ~otheruser | cd ~rahul |
| Current dir (tilde trick) | cd ~+ | Same place (rarely used) |
Common Mistakes & Teacher Warnings
- No space → wrong: cd.. → correct: cd ..
- Typo in name → cd Documets → error: No such file or directory
- Case sensitive → cd documents ≠ cd Documents
- Permission denied → You can’t cd into folders you don’t have permission for (use ls -ld folder to check)
- cd inside script → Changes directory only for that script — terminal stays where it was
- cd – first time in new terminal → sometimes says bash: cd: OLDPWD not set (normal — no previous location yet)
Practice Session Right Now (Do This!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Start fresh cd ~ pwd mkdir -p practice/level1/level2/level3 cd practice/level1/level2/level3 pwd # deep inside cd .. # up one pwd cd ../.. # up two pwd cd /etc pwd cd - pwd # should go back to level3 or similar cd ~ ls -a # see hidden stuff in home |
See how fast you move? That’s the power of cd!
Got it, boss? Any place confusing? Want to know about pushd / popd (advanced cd stack)? Or how to fix cd to auto-run ls after? Or practice more?
Just say — “Teacher, explain cd with pushd popd” or “more cd tricks” or next command like “mkdir” or “pwd deep dive”.
Keep moving around the terminal — soon you’ll feel like you own the building! 🏢🐧 From Hyderabad! 😄
