A terminal window on a Ubuntu-style Linux desktop.
Fatmawati Achmad Zaenuri/Shutterstock

Many Linux folks have never heard of pushd and popd, but they’ve been around forever. They can also dramatically speed up the process of navigating directories on the command line. We’ll walk you through how to use them.

What Are pushd and popd?

One of the innovations Bill Joy incorporated in his 1978 C Shell was the concept of a directory stack and the means to manipulate it: pushd and popd. Imitation being the sincerest form of flattery, the directory stack, pushd, and popd were soon incorporated into other shells (like Bash) and even other operating systems.

The concept of the stack is a simple one. Items are placed on the stack one at a time, with the most recently added item always occupying the top position. When items are retrieved from the stack, they’re removed, in order, from the top downward. Stacks of this nature are often referred to as Last In, First Out (LIFO) queues.

Actually, pushd and popd are a little more flexible than this, but this is a good model to keep in mind for now.

As we’re referring to a directory stack, it probably comes as no surprise that the “d” in pushd and popd stands for “directory.” These commands allow you to push directories onto, or pop them off of, the directory stack.

But how does that benefit us?

How pushd Populates the Stack

When you use pushd, the following three things happen:

  • You change the directory the same as if you’d used cd.
  • The name and path of the directory are added to the stack.
  • The stack is displayed as a space-separated list of directories.

In the following examples, note how the directory stack grows with each new pushd command. Also note that the top of the stack is to the left—this is where the new entries appear.

After the first pushd command, there are two entries in the stack: the directory you left, and the one to which you moved.

For our example, we type the following:

pushd ~/Desktop
pushd ~/Music
pushd ~/Documents
pushd ~/Pictures
pushd ~

The last pushd command took us back to our home directory, so the first and last entries in the stack are the tilde (~), which represents our home directory. This shows that, although a directory is already in the stack, it will be added again for other pushd commands.

Note also that the left-most entry in the stack, which is most recently added entry, is your current directory.

The dirs Command

You can use the dirs command, as shown below, to display the directory stack:


It doesn’t affect the stack, it just displays it. Some of the options you can use with pushd refer to the position of the directories in the stack.

If you want to see the numeric position of each directory, you can use the -v (vertical) option as shown below:

dirs -v

If you’d rather see the spelled-out path to your home directory instead of the tilde (~), add the -l (long format) option, like so:

dirs -v -l

Adding a Directory to the Stack

As we’ve seen, when you use the pushd command, it does three things: changes your directory, adds the new directory to the stack, and displays the stack for you. You can use the -n (no rotation) option to add a directory to the stack without changing the current directory.

Here’s our directory stack:

dirs -v -l

Now, we’ll use the pushd command with the -n option and pas in the /home/dave directory as a parameter. Then, we’ll check the directory stack again.

We type the following:

pushd -n /home/dave
dirs -v -l

The  /home/dave directory was added to the stack in slot 1, which is the second place in the stack. It can’t occupy the top position because slot zero is always the current directory.

We didn’t leave the current directory, ~/Videos, so it wasn’t rotated to another position in the stack.

Changing Directory by Rotating the Stack

You can use numeric parameters with pushd to move to any directory in a stack, and the stack rotates when you do so. The directory you’ve chosen to move then becomes the first entry in the stack.

You reference the directories in the stack by their position number. You can count from the top or bottom of the stack. For positive numbers, such as +3, count from the top; for negative numbers, such as -2, count from the bottom.

The /home/dave/Documents directory is in position three. We can use the following command to move that directory:

pushd +3

The directories in the stack above the directory we’ve chosen are moved to the bottom of the stack. Our chosen directory now occupies the top position and we’re moved into that directory.

If we want to change into the directory at the bottom of the stack, we can use the following command:

pushd -0

The last directory is moved to the first slot, and all the others are moved down in the stack. We’re changed to the ~/Pictures directory.

The popd Command

You can use the popd command to remove directories from the stack.

If we look at the directory stack, we can see that the directory in position 1 is /home/dave. To remove this from the stack, we type the following to pass the number to popd:

dirs -v -l
popd +1

The /home/dave directory was removed, and those that were below it in the stack have each moved up one place.

Just as we can with pushd, we can count from the bottom of the stack with popd. To remove the last directory from the stack, we type:

popd -0

The ~/Music directory is removed from the last position in the stack.

To change the directory, do something, and then hop back to the previous directory, you can use pushd and popd together.

We’ll use pushd to move to a different directory. We’ll use popd to discard the topmost directory in the stack and move to the directory in the second position. This is the directory you just moved out of, so you’re dropped back into the directory you were originally in.

We type the following:

pushd ~

We started in the ~/Projects directory, pushd to the home directory, and then popd back to the ~/Projects directory.

Rotating Through the Entire Stack

We’re going to illustrate how to rotate through a stack with some nested directories, but you could use any directories anywhere in the file system.

Our deepest level of nesting is:


From the home directory, we’ll progressively descend through each directory until we reach the articles directory. Then, we’ll look at the directory stack.

We type the following:

pushd ~/Projects
pushd htg
pushd articles
dirs -v -l

When you repeatedly issue pushd +1 commands, you can cycle round and round through the stack of directories. If you do this often, pushd +1 would be a good candidate for an alias.

Type the following:

pushd +1

RELATED: How to Create Aliases and Shell Functions on Linux

Stamping Over the Stack

It’s easy to revert to old habits and use cd to change directory. If you do that, you’ll stamp over the first directory in the stack. This is inevitable, as the first slot is reserved for the current working directory—none of the others change position.

To do this, type the following:

dirs -v -l
cd ~/Music
dirs -v -l

After you get used to the pushd and popd commands (and, perhaps, use them to create a few aliases), you’ll have a super-fast way to hop between directories.

This is why we hang around the command line. Efficiency rocks, right?

RELATED: 37 Important Linux Commands You Should Know

RELATED: Best Linux Laptops for Developers and Enthusiasts

Profile Photo for Dave McKay Dave McKay
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he is now a full-time technology journalist. During his career, he has worked as a freelance programmer, manager of an international software development team, an IT services project manager, and, most recently, as a Data Protection Officer. His writing has been published by  howtogeek.com, cloudsavvyit.com, itenterpriser.com, and opensource.com. Dave is a Linux evangelist and open source advocate.
Read Full Bio »