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  

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.

Photo Credit: 

flickr / Roberto Verzo

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question
randomness