The pushd command lets you switch directories while saving your current location to a stack. It works with popd and dirs to manage directory history more efficiently than cd.
▶️ Basic Usage
Go to a directory and save current one
pushd /var/log
✔ Moves to /var/log
✔ Saves previous directory to the stack
🔄 Switch Between Directories
Toggle between last two directories
pushd
✔ Quickly jumps back and forth
📚 View Directory Stack
dirs
Example output:
/var/log /home/user
🔙 Return to Previous Directory
popd
✔ Removes the top directory from the stack
✔ Returns you to the last location
🧪 Practical Examples
Work in temp folder, then return
pushd /tmp
# do work
popd
Cycle through multiple directories
pushd /etc
pushd /usr
dirs
⚙️ Useful Options
Don’t change directory, just add to stack
pushd -n /opt
Rotate stack
pushd +1
⚠️ Notes
- Works in bash, zsh, and similar shells
- Stack resets when terminal closes
- Faster than repeatedly typing long paths
🧠 When to Use pushd
- Switching between project folders
- Temporary directory changes
- Shell scripting workflows
💡 Pro Tip
Use pushd + popd instead of cd when multitasking—it saves time and avoids losing your place.