I am frequently surprised by the number of colleagues I have who do not know about the BASH builtins pushd
and popd
. I myself remember seeing them for the first time in a script about 10 years ago, and had to look up the commands. At first I didn’t quite understand the point, but now I use them all the time. It is a simple concept – instead of using cd
to change directories, use pushd
– this will do 2 things: 1) change the directory, and 2) push it onto a stack of all the different working directories you have used. This is really handy when you are in the middle of a task, and need to do something else quickly in a different directory. Once you are done, simply run popd
and then you are back to where you started. You can the see the contents of your directory stack using dirs
.
This can also be used very effectively in BASH scripts. Sometimes certain commands might expect to be run from a certain directory.