Some practical bash today.

In bash there is cd and cd -. Using these two buddies, we can jump between two directories very quick. I use them all the time.

More in the dark there is pushd, popd and dirs. Those I rarely had a usecase for. Until I started digging. There is however a couple of them.

pushd and popd work on an internal stack for directories and allow to alter them and: to expand them.

Usually there is only one entry in them: the current pwd.

$ cd ~
$ dirs -v
0 ~

This single entry is constantly rewritten whenever you change the directory.

$ cd ~
dirs -v
0 ~
$ cd /mnt
$ dirs -v
0 /mnt

With pushd we can put other paths into that stack as well:

bash $ cd ~ dirs -v 0 ~ $ pushd /mnt $/mnt> dirs -v 0 /mnt 1 ~ `

The directory /mnt got placed on the top of the stack without overwriting the exiting value ~. That one got pushed to the index level 1 instead. Also notice that the current path has changed from ~ to /mnt. This is something pushd and popd do. Use pushd/popd -n <dir> if you do not want that. With every change in the path, the index 0 is getting overwritten, while the index 1 in the stack keeps consistent.

This is something that can be used to quickly jump between multiple directorys. The index can be address like cd ~1 to jump to ~. With only one or two entries in the stack, it’s not that big advantage over the use of cd and cd -.

But with a couple of directories initially put in the stack, this can speed up directory navigation quite fast.

You empty the stack with popd. popd will also by default change to the directory you just popd.

pushd and popd are both bash builtins. Therefore the documentation points to the bash man page.