Go Back to Previous Directory via Command Line
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
To go back to the previous directory in a command-line interface, use the command cd - (with a hyphen). This works in Bash, Zsh, and most Unix-like shells, instantly returning you to the last directory you were in before your current one.
What Does cd - Actually Do?
The cd command (short for “change directory”) is a shell built-in, not a separate program. When you type cd -, the shell looks at a special environment variable called OLDPWD, which stores the path of the directory you were in immediately before the most recent cd. It then changes to that directory and prints the new path so you can see where you are.
This is different from cd .., which moves up one level in the directory tree (from /home/user/docs to /home/user). The hyphen version is not about hierarchy—it’s about history. If you were in /var/log and then jumped to /etc, cd - takes you back to /var/log, even though /etc is not a subdirectory of it.
Why Use cd - Instead of Typing the Full Path?
It saves time and avoids typos. If you frequently switch between two deep directories, like /home/user/projects/website/src/components and /home/user/projects/website/docs, typing the full path each time is tedious. cd - toggles between the two. You can also use it in scripts to return to a starting point after temporarily moving somewhere else.
Another advantage is that it works even if the previous directory was deleted or renamed—though in that case you’ll get an error. The shell remembers the string, but if the path no longer exists, the command fails.
How to Use cd - in Practice
The syntax is simple:
- Type
cd -and press Enter. - The shell changes to the previous directory and prints the new working directory.
- Typing
cd -again returns you to the original directory, so you can toggle back and forth.
For example:
- You are in
/home/user. - You run
cd /tmp. - You run
cd -. The shell prints/home/userand you are now there. - Run
cd -again. The shell prints/tmpand you are back.
What About Windows Command Prompt and PowerShell?
Windows Command Prompt (cmd.exe) does not support cd -. Instead, you can use pushd and popd to manage directory stacks. pushd \\server\share changes to that directory and saves the previous one; popd returns to it. PowerShell also supports pushd and popd, and it has an alias for cd called Set-Location. In PowerShell, you can also use cd - to go to the previous location, because PowerShell treats the hyphen as a special parameter that refers to the last location in its location stack.
Common Mistakes and How to Avoid Them
One common mistake is confusing cd - with cd ~. The tilde (~) is shorthand for your home directory, not the previous directory. So cd ~ always takes you to /home/yourname, while cd - takes you to wherever you were before.
Another error is expecting cd - to work after using cd in a subshell or a script. Each shell session has its own OLDPWD. If you run a script that changes directory, the parent shell’s OLDPWD is not updated unless you source the script (run it with . or source).
Also note that cd - prints the directory path to the terminal. If you want to suppress that output, you can redirect it: cd - > /dev/null (in Bash).
Alternatives: pushd and popd
While cd - only remembers one previous directory, pushd and popd maintain a stack of directories. You can push multiple directories and then pop back through them in reverse order. This is useful when you need to visit several locations and return to earlier ones without typing long paths.
Example:
pushd /var/log– changes to /var/log and pushes it.pushd /etc– changes to /etc and pushes it.popd– returns to /var/log and removes it from the stack.popd– returns to the original directory.
You can also use dirs to display the current stack.
Frequently Asked Questions
Does cd - work in all shells?
It works in Bash, Zsh, Ksh, and most POSIX-compliant shells. It does not work in the original Bourne shell (sh) on some older systems, but modern sh implementations often support it. In fish shell, you can use cd - as well, but fish also offers cdh to show a history of directories.
What if I want to go back two directories?
cd - only goes back one. To go back two, you can use cd ../.. (go up two levels) or use pushd twice and then popd twice. Some shells allow cd -2 but that is not standard; it’s better to use pushd/popd for multiple steps.
Can I use cd - in a script?
Yes, but be aware that each script runs in its own subshell, so the OLDPWD is local to that subshell. If you want the script to affect the parent shell, you must source it (e.g., . script.sh).
Why does cd - sometimes print the directory?
That’s by design. The shell prints the new working directory so you can confirm where you are. If you don’t want to see it, you can redirect stdout to /dev/null, but it’s usually helpful to see it.
Is there a way to see the last few directories I visited?
Yes, in Bash you can use dirs -v to list the directory stack, but that only shows the stack you’ve built with pushd. For a broader history, you can use cd - repeatedly, but it only remembers the immediate previous directory. Tools like autojump or z can track frequent directories, but they require installation.
What if I accidentally type cd - and it fails?
If the previous directory no longer exists, the shell will show an error like bash: cd: /path: No such file or directory. In that case, you’ll need to navigate manually. The OLDPWD variable still holds the old path, but you can’t use it if the directory is gone.
Related
Formula used
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.