Effortless Directory Navigation with `cd -` and `pushd`/`popd`
Quick Tip
Effortless Directory Navigation with `cd -` and `pushd`/`popd`
Challenge: You frequently jump between a few directories and find yourself typing long paths repeatedly or struggling to remember where you were before. This leads to wasted time and potential typos.
The Solution: Utilize built-in shell features for quick directory switching. The `cd -` command instantly returns you to the previous directory, and the `pushd` and `popd` commands allow you to manage a stack of directories.
# To switch back to the previous directory: cd - # To add a directory to the stack and switch to it: pushd /path/to/new/directory # To return to the directory you pushed before: popd # To see your directory stack: dirs
Why it works: `cd -` is a shortcut that refers to the `$OLDPWD` environment variable, which stores the path of the directory you were in previously. `pushd` adds the current directory to a stack and then changes to the specified directory, while `popd` removes the top directory from the stack and changes to it, effectively letting you cycle through a history of directories you’ve visited.
Pro-Tip: Use `pushd +N` to switch to the Nth directory in the stack (e.g., `pushd +2` switches to the second directory from the top).
Linux Tips & Tricks | © ngelinux.com | 5/2/2026
