How to use pushd and popd on Linux

Lots of Linux folks have never heard of it pushd and popd , but they’ve been around forever. You can also use the command line to make navigating directories much faster. We’ll show you how to use them.

What are pushd and popd?

One of the innovations Bill Joy incorporated into his 1978 C-shell was the concept of a directory stack and the means to manipulate it: pushd and popd . Imitation is the most sincere form of flattery, the directory pile, pushd , and popd soon became integrated with other shells (like Bash) and even other operating systems.

The concept of the stack is simple. Items are placed on the stack one after the other, with the item added last always at the top. As items are removed from the stack, they are removed in sequence from top to bottom. Stacks of this type are often called In last, out first (LIFO) tails.

Strictly speaking, pushd and popd are a bit more flexible than these, but this is a good model to remember for now.

Since we’re referring to a directory stack, it’s probably no surprise that the “d” in pushd and popd stands for “directory”. You can use these commands to move directories onto or from the directory stack.

But what use is that to us?

How pushd fills the stack

If you use pushd , the following three things happen:

  • You change the directory just as if you had used it cd .
  • The name and path of the directory are added to the stack.
  • The stack appears as a space-separated list of directories.

In the following examples, notice how the directory stack grows with each new one pushd Command. Also notice 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, let’s enter:

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

The last pushd Command brought us back to our home directory, so the first and last entries in the stack are the tilde ( ~ ), which is our home directory. This shows that even though a directory is already on the stack, it will be added back for others pushd Commands.

Also notice that the entry on the far left of the stack, the 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’s just displayed. 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 instead of the tilde ( ~ ), Add the -l (long format) option, as follows:

                      dirs -v -l

Add a directory to the stack

As we’ve seen, you use the pushd Command, it does three things: change your directory, add the new directory to the stack, and display 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 is our directory stack:

                      dirs -v -l

Now let’s use that pushd Command with the -n option and pas in the /home/dave Directory as a parameter. Then we check the directory stack again.

We enter the following:

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

the /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 slot zero is always the current directory.

We haven’t left the current directory, ~/Videos so that it was not rotated to another position in the stack.

Change directory by rotating the stack

You can use numeric parameters with. use pushd to go to any directory on a stack, and the stack spins in the process. The directory to be moved then becomes the first entry in the batch.

They refer to the directories in the stack by their position number. You can count from the top or bottom of the stack. With positive numbers like +3 you count from above; for negative numbers like -2, count from the bottom.

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

                      pushd +3

The directories in the stack above the directory we selected will be moved to the bottom of the stack. Our selected directory now takes the top position and we are moved into this directory.

If we want to go 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 others are moved down the stack. We have moved 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 is in position 1 /home/dave . To remove this from the stack, let’s add the following to the number. to hand over popd :

                      dirs -v -l
                      popd +1

the /home/dave Directory has been removed, and those that were below in the stack have moved up one place at a time.

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

                      popd -0

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

To change the directory, do something, then jump back to the previous directory pushd and popd together.

We use pushd to change to another directory. We use popd to discard the top directory in the stack and switch to the directory in the second position. This is the directory you were just moved from, so you will fall back into the directory you were originally in.

We enter the following:

                      pushd ~

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

Rotate through the entire stack

We’ll demonstrate how to rotate through a stack with a few nested directories, but you can use any directory anywhere in the filesystem.

Our deepest nesting level is:


From the home directory, we step through each directory until we reach the article directory. Then we look at the directory stack.

We enter the following:

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

When you spend repeatedly pushd +1 You can use commands to go through the stack of directories. If you do this frequently, pushd +1 would be a good candidate for an alias.

Enter the following:

                      pushd +1

How to create aliases and shell functions on Linux

Stamp over the pile

It’s easy to go back to old habits and use cd Change directory. When you do this, you will overwrite the first directory on the stack. This is inevitable because the first slot is reserved for the current working directory – none of the others will change position.

To do this, enter the following:

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

After you get used to it, pushd and popd Commands (and maybe use them to create a few aliases) are a super fast way to switch between directories.

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

37 Essential Linux Commands You Should Know

Linux commands
Files tar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · Rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processes alias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networking netstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts