Skip to main content

How to Use pushd and popd on Linux

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 pushdcommand. 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 "pushd ~/Desktop," "pushd ~/Music," "pushd ~/Documents," "pushd ~/Pictures," and "pushd ~" commands in a terminal window.
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:
dirs
The "dirs" in a terminal window.
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
The "dirs -v" command in a terminal window.
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
The "dirs -v -l" command in a terminal window.

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
The "dirs -v -l" command in a terminal window.
Now, we’ll use the pushd command with the -n option and pas in the /home/davedirectory as a parameter. Then, we’ll check the directory stack again.
We type the following:
pushd -n /home/dave
dirs -v -l
The "pushd -n /home/dave" and "dirs -v -l" commands in a terminal window.
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 "pushd +3" command in a terminal window.
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 "pushd -0" command in a terminal window.
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 "dirs -v -l" and "popd +1" commands in a terminal window.
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 "popd -0" command in a terminal window.
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 ~
popd
The "pushd ~" and "popd" commands in a terminal window.
We started in the ~/Projects directory, pushd to the home directory, and then popdback 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:
/home/dave/Projects/htg/articles
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
The "pushd ~/Projects," "pushd htg," "pushd articles," and "dirs -v -l" commands in a terminal window.
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
The "pushd +1" command in a terminal window.

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
The "dirs -v -l," "cd ~/Music," and "dirs -v -l" commands in a terminal window.

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.

Comments

Popular posts from this blog

4 Ways to Communicate Across Browser Tabs in Realtime

1. Local Storage Events You might have already used LocalStorage, which is accessible across Tabs within the same application origin. But do you know that it also supports events? You can use this feature to communicate across Browser Tabs, where other Tabs will receive the event once the storage is updated. For example, let’s say in one Tab, we execute the following JavaScript code. window.localStorage.setItem("loggedIn", "true"); The other Tabs which listen to the event will receive it, as shown below. window.addEventListener('storage', (event) => { if (event.storageArea != localStorage) return; if (event.key === 'loggedIn') { // Do something with event.newValue } }); 2. Broadcast Channel API The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and  Web Workers . One Tab can create and post to a channel as follows. const channel = new BroadcastChannel('app-data'); channel.postMessage(data); And oth...

Certbot SSL configuration in ubuntu

  Introduction Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free  TLS/SSL certificates , thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx. In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and set up your certificate to renew automatically. This tutorial will use a separate Apache virtual host file instead of the default configuration file.  We recommend  creating new Apache virtual host files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration. Prerequisites To follow this tutorial, you will need: One Ubuntu 18.04 server set up by following this  initial ...

Working with Node.js streams

  Introduction Streams are one of the major features that most Node.js applications rely on, especially when handling HTTP requests, reading/writing files, and making socket communications. Streams are very predictable since we can always expect data, error, and end events when using streams. This article will teach Node developers how to use streams to efficiently handle large amounts of data. This is a typical real-world challenge faced by Node developers when they have to deal with a large data source, and it may not be feasible to process this data all at once. This article will cover the following topics: Types of streams When to adopt Node.js streams Batching Composing streams in Node.js Transforming data with transform streams Piping streams Error handling Node.js streams Types of streams The following are four main types of streams in Node.js: Readable streams: The readable stream is responsible for reading data from a source file Writable streams: The writable stream is re...