How to use pushd and popd in Linux

Contents

A terminal window on an Ubuntu-style Linux desktop.

Many Linux folks have never heard of pushd and popd, but they have been around forever. They can also dramatically speed up the command line directory navigation procedure. We will show you how to use them.

What are pushd and popd?

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

The concept of stack is simple. Items are placed on the stack one at the same time, and the most recently added item always occupies the top position. When items are retrieved from the stack, are eliminated, an order, from top to bottom. Stacks of this nature are often referred to as Last in first out (LIFO) colas.

Truly, pushd and popd they are a bit more flexible than this, but this is a good model to pay attention to for now.

How we refer to a directory stack, it's probably not surprising that the “d” on pushd and popd it means “directory”. These commands allow you to insert directories or remove them from the directory stack.

But, How does that benefit us?

How pushd fills the stack

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 space separated list of directories.

In the following examples, watch the directory stack grow 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 one that moved.

For our example, we write the following:

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

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

Also note that the leftmost entry in the stack, which is the most recently added entry, is your current directory.

The dirs command

You can use the dirs command, as it's shown in the following, to display the directory stack:

dirs

Does not affect the stack, just the sample. Some of the alternatives you can use with pushd check 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) as it's shown in the following:

dirs -v

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

dirs -v -l

Add a directory to the stack

As we have seen, when you use the pushd command, does three things: change your directory, add the new directory to the stack and show the stack for you. You can use the -n (no rotation) to add a directory to the stack without changing the current directory.

Here is our directory stack:

dirs -v -l

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

We write the following:

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

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

We do not leave the current directory, ~/Videos, so it was not rotated to another position in the stack.

Directory change by rotating the stack

You can use numeric parameters with pushd to move to any directory on a stack, and the stack spins when it does. The directory you have chosen to move becomes the first entry on the stack.

Refers to directories on the stack by their position number. Can count from the top or bottom of the stack. For positive numbers, What +3, count from above; for negative numbers, What -2, count down.

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

pushd +3

The directories on the stack above the directory we have chosen are moved to the end of the stack. Our chosen directory now occupies the first position and we move to that 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 slot and all the others are moved down the stack. We switch 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 at position 1 it is /home/dave. To clear this off the stack, we write the following to pass the number to popd:

dirs -v -l
popd +1

the /home/dave directory was removed, and those who were below him in the pile have moved up one place.

In the same way that we can with pushd, we can count from the bottom of the stack with popd. To delete the last directory on the stack, we wrote:

popd -0

the ~/Music The directory is removed from the last position on 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 a different directory. we will use popd to drop the top directory on the stack and move to the directory in the second position. This is the directory you just came out of, so you are returned to the directory you were originally in.

We write the following:

pushd ~
popd

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

Spin through the whole stack

We are going to illustrate how to rotate through a stack with some nested directories, but you can use any directory anywhere on the filesystem.

Our deepest level of nesting is:

/home/dave/Projects/htg/articles

From home directory, we will progressively descend through each directory until we reach the posts directory. After, we will see the directory stack.

We write the following:

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

When it broadcasts repeatedly pushd +1 commands, can loop over and over the directory stack. If you do this often, pushd +1 would be a good candidate for an alias.

Write the following:

pushd +1

RELATED: How to create aliases and shell functions in Linux

Stamped on the stack

It's easy to go back to old habits and use cd to change directory. If it does, will seal the first directory on the stack. This is unavoidable, since the first space is reserved for the current working directory; none of the others change position.

To do this, write the following:

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


After you get used to the pushd and popd commands (and, maybe, use them to create some aliases), you will have a super fast way to jump between directories.

That is why we stay on the command line. Efficiency rocks, truth?

RELATED: 37 Important Linux Commands You Should Know About

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.