How to Use pushd and popd on Linux

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

Many Linux users have never heard of pushd and popd, but they have been around forever. They can also significantly speed up the process of navigating directories on the command line. We will explain 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 how to handle it: pushd and popd. Imitation being the most sincere form of flattery, the stack of directories, pushd and popd were quickly incorporated into other shells (like Bash) and even into other operating systems.

The concept of the battery is simple. The objects are placed on the stack one by one, the last element added always occupying the first position. When items are retrieved from the stack, they are deleted, in order, from top to bottom. Batteries of this nature are often called Last in, first out (LIFO) queues.

In fact, pushd and popd are a little more flexible than that, but it’s a good model to keep in mind for now.

As we are talking about a directory stack, it is probably not surprising that the “d” in pushd and popd means “directory”. These commands allow you to push or remove directories from the directory stack.

But how does it benefit us?

How pushd fills the pile

When you use pushd, the following three things happen:

You change the directory as if you had used cd.
The directory name and path are added to the stack.
The stack is displayed as a list of directories separated by spaces.

In the following examples, note the growth of the directory stack with each new pushd command. Also note that the top of the stack is on 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 directory you moved to.

For our example, we type the following:

pushd ~ / Office
pushd ~ / Music
pushd ~ / Documents
pushd ~ / Images
pushd ~

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

Also note that the leftmost entry in the stack, which is the last entry added, 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 on the stack.

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

dirs -v

If you prefer to see the spelled path to your home directory instead of the tilde (~), add the -l option (long format), like this:

dirs -v -l

Adding a directory to the stack

As we have 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 option (no rotation) to add a directory to the stack without modifying the current directory.

Here is our stack of directories:

dirs -v -l

Now let’s use the pushd command with the -n option and not in the / home / dave directory as a parameter. Then we will check the directory stack again.

We type the following:

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

The / home / dave directory has been added to the stack in location 1, which is the second place in the stack. It cannot occupy the first position because the zero location is always the current directory.

We have not left the current directory, ~ / Videos, so it has not been turned to another position in the stack.

Change directory by rotating the stack

You can use numeric parameters with pushd to move to any directory in a stack, and the stack spins when you do so. The directory you have 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 this directory:

pushd +3

The stack directories above the directory we chose are moved to the bottom of the stack. The directory we have chosen now occupies the first place and we are transferred to this directory.

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

pushd -0

The last directory is moved to the first location and all the others are moved down in the stack. We moved to the ~ / Pictures directory.

The popd command

You can use the popd command to delete 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 has been deleted, and those below in the stack have each moved up one place.

Just like we can do with pushd, we can count from the bottom of the stack with popd. To delete the last directory from the stack, we type:

popd -0

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

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

We will use pushd to move to another directory. We will use popd to remove the top directory from the stack and move to the second directory. This is the directory you just left, so you are returned to the directory in which you were originally.

We type the following:

pushd ~

We started in the ~ / Projects directory, pushed to the base directory, then returned to the ~ / Projects directory.

Rotation across the entire stack

We will illustrate how to scroll through a stack with certain nested directories, but you can use any directory anywhere in the file system.

Our deepest level of nesting is:

/ home / dave / Projects / htg / articles

From the personal directory, we will gradually descend into each directory until we reach the articles directory. Next, we will examine the directory stack.

We type the following:

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

When you repeatedly issue pushd +1 commands, you can cycle through the directory stack. If you do it often, pushd +1 would be a good candidate for a a.k.a.

Type the following:

pushd +1

RELATED: How to Create Aliases and Shell Functions on Linux

Stamping on the stack

It’s easy to go back to old ways and use cd to change directories. In this case, you will buffer the first directory in the stack. This is inevitable, since the first location is reserved for the current working directory – none of the others changes position.

To do this, type the following:

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

Once you’ve gotten used to the pushd and popd commands (and, perhaps, used them to create a few aliases), you’ll have a super-fast way to browse the directories.

That’s why we hang out the command line. Efficiency rocks, right?

RELATED: 37 important Linux commands you should know

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.