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.

The pushd and popd commands put directory paths onto a directory stack (pushd) and then pop them off again (popd). These commands provide a way for you to more easily navigate between a number of locations in your file system. You can add a series of paths onto your stack and then navigate to them in the reverse order. For some Unix users, this kind of operation saves them a lot time moving around in their file systems. Before this way of moving around file systems is likely to have much appeal, it helps to know a little about how stacks work. For this, picture a pile of dishes. A push operation is like adding a plate to the top of the pile or "stack" of dishes. A pop operation is the reverse. It's like grabbing the top plate off the pile. Some like to refer to this type of operations model as "LIFO" (last in, first out) structure. In other words, the plate you have most recently added to the stack is the first to be pulled off. If it were really a stack of dishes, we wouldn't start out with an empty stack and the plates on the bottom might never be used. With Unix systems, however, the directory stack always starts out empty. The pushd command (e.g., pushd /tmp) changes your current directory to the location specified and stores the new directory on your directory stack at the same time. The popd command then removes the top path from your directory stack and then returns you to the previous directory -- the one below it on your stack. So, when you first log in, your directory stack is empty. If you issue the dirs command to display the stack, you just see your current directory which is, of course, your home.

$ dirs
~

If you move to a different directory, however, the result of the dirs command illustrates that you're now somewhere else. You can think of this as the place to which you will return if you issue a pushd and then a popd command. The dirs command, after moving to a different directory, displays only that directory. If you would issue a set of pushd and popd commands at this point, this is where you would find yourself when you were done.

$ cd /tmp
$ dirs
/tmp

using the pushd and popd commands is not unlike dropping directory "bread crumbs" and then returning in reverse order to all the places you visited. In this example, we move through three directories (given simple names to make the example clear). As each pushd (add directory) command is issued, the pushd command displays the current stack.

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

As you can see, the display shows each new directory being added to the leftmost position in the output. 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". This same kind of operation on a Unix system might look like this:

#!/bin/bash

if [ $# != 1 ]; then
    echo "try harder"
    exit 1
fi

pushd $1
ls
popd

Manipulating your directory stack might prove to be a new favorite trick or might seem more trouble than it's worth, but many admins find it's a useful way to move between directories with ease.

Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.

What’s wrong? The new clean desk test
Join the discussion
Be the first to comment on this article. Our Commenting Policies