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

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...