Last Updated: February 25, 2016
·
753
· spanneberg

Use pushd and popd to easily jump back to previous folders

Often you have a situation that you are in a certain folder (say your Tomcat directory) and want to naviagate to somewhere else because you may need to adjust another component on the same host.

I see a lot of people doing the following then:

> cd ../../somewhere/iwanttogohere

and then cd'ing back once they are done

> cd ../../whereiwas/before

If you instead use pushd in the first command

> pushd ../../somewhere/iwanttogohere

your current working directory will be put on a stack before the directory is changed. Once you type

> popd

you simple jump to the directory that is currently at the top of the stack.

If you add several folders to the stack, you can watch the current state with

> dirs

and manipulate the stack with the +N and -N arguments of pushd and popd where N is the number in the list of directories on your stack.

See also http://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html for a detailed documentation.