Python Changing Directories with cd
The `cd` (change directory) command alters the shell current working directory. Understanding shortcuts like `cd ~`, `cd -`, `cd ..`, and directory stacks via `pushd` and `popd` accelerates command line productivity.
"`cd` is like stepping into a different room in a building. `pushd` and `popd` are like leaving breadcrumbs on the floor so you can instantly teleport back to your previous room."
Deep Dive: How It Works
Shell Builtin: `cd` is a builtin because a child process cannot modify the working directory of its parent shell.
Directory Stack: `pushd <dir>` changes directory and saves current path onto a LIFO stack; `popd` jumps back to the top of stack.
CDPATH Variable: You can configure a colon-separated search path for `cd` to quickly jump to frequent project dirs.
Syntax Blueprint
cd path/to/dir # Move to specific path cd .. # Move up one level cd - # Switch to previous directory pushd /var/log # Push and switch popd # Pop and return
Use cd for simple directory traversal and pushd/popd for stack-based history traversal.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using cd inside a subshell `(cd /tmp)` and expecting the parent shell to stay in /tmp.Why it happens: Subshells execute in an isolated child process; changes do not persist to the outer terminal.
How to fix: Run `cd` directly or source scripts with `.` or `source`.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Switch to Previous Directory with cd -
Create `/tmp/dir_one` and `/tmp/dir_two`.
Navigate into `/tmp/dir_one`, then into `/tmp/dir_two`.
Run `cd - > /dev/null` to return to `/tmp/dir_one` and print `pwd`.
Clean up created directories.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.