Saltar al contenido

How to use pushd and popd in Linux

How to use pushd and popd in Linux

What are pushd and popd?

One of the innovations that Bill joy embedded in his C shell 1978 was the concept of directory stack and means of manipulation: pushdYes popd. Imitation is the most sincere form of flattery, the stack of directors pushd, Y popdthey were soon incorporated into other shells (such as Bash) and even into other operating systems.

The stack concept is simple. The items are placed on the stack one by one, the most recently added item always being in the top position. When items are retrieved from the stack, they are removed, in order, from top to bottom. Stacks of this nature are often called queues of last entry, first exit (LIFO).

Actually, pushdYes popdthey are a little more flexible than this, but this is a good model to consider for now.

Since we are referring to a stack of directories, it is probably not surprising that «d» from pushdand to be popdacronym for «director». These commands allow you to insert directories or remove them from the directory stack.

But how does this benefit us?

How pushd fills the stack

When you use it pushd, the following three things happen:

  • Change the directory in the same way as if you had used it CD.
  • The name and path of the directory are added to the stack.
  • The stack is displayed as a list of space-separated directories.

In the following examples, notice how the directory stack grows with each new one pushdcommand. Also note that the top of the stack is on the left; new entries appear here.

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

For our example, we write the following:

pushd ~ / Escritorio
pushd ~ / Música
pushd ~ / Documentos
pushd ~ / Imágenes
empujado ~

The last one pushdthe command took us back to our root directory, so the first and last entries in the stack are tilde ( ~), which is our CEO. This shows that although a directory is already in the stack, it will be added again for others pushdorders.

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

Order dirs

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


"Directories" in a terminal window.

It does not affect the stack, only the sample. Some of the options you can use are pushdrefers to the position of the directories on the stack.

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

dirs -v

The "dirs -v" command in a terminal window.

If you prefer to see the spelled path to the home directory instead of the tilde ( ~), he adds itoption (long format), as follows:

dirs -v -l

The "dirs -v -l" command in a terminal window.

Add a directory to the stack

As we have seen, when you use pushdcommand, do three things: change your directory, add the new directory to the stack, and display the stack for you. You can use -noption (without rotation) to add a directory to the stack without changing the current directory.

Here is our stack of directories:

dirs -v -l

The "dirs -v -l" command in a terminal window.

Now, we’ll use pushdcommand with the -n option and step in / home / davedirectory as a parameter. Then we’ll check the directory stack again.

We write the following:

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

The "pushd -n / home / dave" and "dirs -v -l" commands in a terminal window.

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

We’re not leaving the current director ~ / Videos, so it was not rotated to another position in the stack.

Changing the directory by rotating the stack

You can use numeric parameters with pushdto move to any directory in a stack, and the stack rotates when it does. The directory you chose to move becomes the first entry in the stack.

You refer to 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 down.

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

pushd +3

Command "pushd +3" in a terminal window.

The folders in the stack above the folder we chose are moved to the bottom of the stack. Our chosen director now occupies the first position and we move to that director.

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

pushd -0

Command "pushd -0" in a terminal window.

The last folder moves to the first slot and all the others move down in the stack. Move on ~ / Imagesdirector.

Popd command

You can use popdcommand to remove directories from the stack.

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

dirs -v -l
popd +1

The "dirs -v -l" and "popd +1" commands in a terminal window.

/ home / davethe directory was removed, and those below it on the stack were moved to a location.

As we can do with pushd, we can count from the bottom of the stack with popd. To remove the last directory from the stack, write:

popd -0

Command "popd -0" in a terminal window.

~ / Musicthe directory is removed from the last stack position.

To change the directory, do something and then go back to the previous directory you can use pushdYes popdtogether.

We will use pushdto move to another directory. We will use popdto throw the top folder off the stack and move to the folder in the second position. This is the directory you just moved from, so it returns it to the directory it was originally in.

We write the following:

empujado ~

The "pushd ~" and "popd" commands in a terminal window.

We start in ~ / Projectsdirector, pushdto the home directory and then popdback to ~ / Projectsdirector.

Rotate through the entire stack

We’ll show you how to rotate a stack with a few nested directories, but you can use any directory anywhere in the file system.

The deepest nesting level is:

/ inicio / dave / Proyectos / htg / articulos

From the main directory, we will progressively go down through each directory until we reach the article directory. Then we’ll look at the directory stack.

We write the following:

pushd ~ / Proyectos
pushd htg
artículos pushd
dirs -v -l

The commands "pushd ~ / Projects", "pushd htg", "articles pushd" and "dirs -v -l" in a terminal window.

When it broadcasts pushd +1orders repeatedly , you can browse the directory stack. If you do this frequently, pushd +1would be a good candidate for a alias.

Write the following:

pushd +1

Command "pushd +1" in a terminal window.

Stamped on the stack

It’s easy to go back to your old habits and use them CDto change the directory. This will seal the first directory in the stack. This is inevitable, because the first space is reserved for the current working directory; none of the others change position.

To do this, type the following:

dirs -v -l
cd ~ / Música
dirs -v -l

The "dirs -v -l", "cd ~ / Music" and "dirs -v -l" commands in a terminal window.

Once you get used to orders pushdYes popd(and maybe use them to create some aliases), you’ll have a very quick way to jump between directories.

That’s why we stay inside command line . Efficiency rocks, right?