brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: cd – takes you back to the last directory

Linux
~/tips/tip-linux-cd-dash

cd /var/log/nginx

Switches back to the directory you were in before the last cd.

Small things first. If you jump between two directories all day, you can stop typing long paths.

cd - changes to the directory you were in before and prints its path. Run it again and you go back. It works as a toggle between two places.

$ cd /var/log/nginx
$ cd ~/projects/site
$ cd -
/var/log/nginx

Bash keeps that previous directory in the variable $OLDPWD, which you can also use in other commands, for example cp $OLDPWD/error.log . to copy a file from where you just were.

The catch is that it only remembers one place. Go three directories deep and the older ones are gone. For a real stack of directories, pushd and popd are built for that. For the two-place shuffle, cd - is all I need.

It is a tiny thing, but typing a deep path ten times a day gets old, and I would rather have a trick for that than a pile of shell aliases to maintain.

Further reading: Bash manual: Bourne Shell Builtins (cd), On this site: type sudo !! when you forgot the sudo, On this site: Alt+. pastes the last argument of your previous command

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link