Unix: Using pushd and popd for faster navigation

Pushing and popping might not be your idea of a good time, but some Unix admins swear that these operations make working in multiple directories as easy as 1, 2, 3, 2, 1.

By  

When looking at the directory stack with the dirs command, consider the rightmost directory in your stack as the bottom.

We could do the same thing like this:

$ pushd ~/dir1; pushd ~/dir2; pushd ~/dir3
~/dir1 ~
~/dir2 ~/dir1 ~
~/dir3 ~/dir2 ~/dir1 ~

Once we start issuing popd (remove directory) commands, we see that the directory stack tosses off the directory on the top of the stack as it retraces its steps back through the directories that were previously visited.

$ popd; popd; popd
~/dir2 ~/dir1 ~
~/dir1 ~
~

Pop until your stack is empty and you will find yourself back where you began.

The pushd and popd command also allow -# and +# arguments (e.g., pushd +1).

$ pushd ~/dir1
~/dir1 ~
$ pushd ~/dir2
~/dir2 ~/dir1 ~
$ pushd ~/dir3
~/dir3 ~/dir2 ~/dir1 ~
$ dirs
~/dir3 ~/dir2 ~/dir1 ~

If you use the pushd +1 command, you rotate the stack in such a way that the top entry moves to the bottom -- and moves you into that directory as well.

$ pushd +1
~/dir2 ~/dir1 ~ ~/dir3
$ pwd
/home/shs/dir2
$ pushd +1
~/dir1 ~ ~/dir3 ~/dir2
$ pwd
/home/shs/dir1
$ pushd +1
~ ~/dir3 ~/dir2 ~/dir1
$ pwd
/home/shs

This kind of strategy allows you to repeatedly move through a series of directories (any number of them) in circular fashion. The first directory shown in the pushd +1 output displays the directory in which you are currently located.

$ pushd ~/dir1; pushd ~/dir2; pushd ~/dir3
~/dir1 ~
~/dir2 ~/dir1 ~
~/dir3 ~/dir2 ~/dir1 ~
$ pwd
/home/shs/dir3
$ pushd +1
~/dir2 ~/dir1 ~ ~/dir3
$ pushd +1
~/dir1 ~ ~/dir3 ~/dir2
$ pushd +1
~ ~/dir3 ~/dir2 ~/dir1
$ pushd +1
~/dir3 ~/dir2 ~/dir1 ~
$ pushd +1
~/dir2 ~/dir1 ~ ~/dir3
$ pushd +1
~/dir1 ~ ~/dir3 ~/dir2
$ pushd +1
~ ~/dir3 ~/dir2 ~/dir1
$ pushd +1
~/dir3 ~/dir2 ~/dir1 ~

You can also use other arguments with pushd, such as pushd +2, to perform this rearrangement of the directory stack multiple times.

If you want to move between multiple directories in random order (presumably without typing the full paths), you might find it easier to create symbolic links and move through them using commands such as cd ~/configs and cd ~/data or you might prefer to create variables that contain the paths you want to visit such as $configs, $data, and $maps, each containing the path to the directory that you need to work in.

Interestingly, even Windows has adopted pushd and popd commands as this simple batch file illustrates:

@echo off
rem show directory listing for folder passed as an argument
echo %1
pushd %1
dir
popd

This batch file would move into whatever directory is supplied as an argument, display a file listing, and then return, as the poets might say, "from whence it came".

Photo Credit: 

flickr / Roberto Verzo

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question