Did you know?
cd - takes you back to the previous directory.
Perfect for jumping between two directories, like a config folder and a log folder.
- Shell & terminal
- Last checked
Explained
When you work in two places at once, for example /etc/nginx/sites-available and /var/log/nginx, you don’t need to type the paths over and over. cd - switches to the directory you were in before and prints its path:
cd /etc/nginx/sites-availablecd /var/log/nginxcd -
/etc/nginx/sites-available
Run cd - again to go back. The shell keeps the previous directory in the OLDPWD variable, so you can also use it in other commands, for example cp error.log "$OLDPWD".
If you need to remember more than one directory, use the directory stack: pushd /path changes directory and remembers the current one, popd returns to it, and dirs -v lists the stack. Git has the same idea: git switch - returns to the previously checked-out branch.
Learn more
-
cdcommand cheat sheet Options and copy-ready examples
