Navigating Your Directory Stack with Pushd and Popd: A Faster Way to Traverse Your Operating System

Use pushd and popd instead of cd

How to navigate your operating system with fewer keystrokes in Bash or similar command line shell.

When navigating the directory tree in a command line shell, it's easy to get lost in the sea of directories. Most of us are used to using the cd command to move around, but there are other commands that can make the process a lot smoother and faster.

The popd and pushd commands are two such commands that can help you traverse your directory stack with ease. These commands work in most command shells and provide a way to store and retrieve directories from the stack, so you don't have to type out full paths every time.

Now, if you're like me, you might be comfortable using cd to navigate your way through the directories, but every now and then, it can be a bit tedious and time-consuming. This is where popd and pushd come in handy.

What is the directory stack?

Before we dive into how to use popd and pushd, let's first talk about what a directory stack is. Think of it as a history of all the directories you've visited in the current command session. Every time you move to a new directory using cd, that directory gets stacked on top of the previous one.

To see what this looks like, you can type the dirs command into your shell. This will give you a list of all the directories that are currently on the stack. (copy and paste if you like)


There is also a more verbose output. The flag -v tells the command we want the verbose output which includes each path’s index in the stack. The flag -l tells the command we want the long or full paths in the output.

dirs -v -l

This works pretty much like web browser history.

How to use pushd and popd

Now that we know what the directory stack is, let's see how we can use pushd and popd to navigate our way through it.

The pushd command allows you to add a directory to the top of the stack and move to it in one step. For example, if you want to move to the directory /home/user/documents and add it to the stack, you can type:

pushd /home/user/documents

This will add /home/user/documents to the top of the stack and move you to that directory. This also works with relative paths. I only used a full path here for clarity.

To move to the next directory on the stack, you can use the popd command. This will remove the top directory from the stack and move you to the next one. For example, if you want to move back to the previous directory on the stack, you can type:


This will remove /home/user/documents from the stack and move you back to the previous directory. This is like the “back” button in our favorite web browser.

You can do fancier maneuvers than just these examples, but that usually just twists my mind in knots and has me back to cding my way around the directory tree again.


In summary, using popd and pushd can be a great way to navigate your way through the directory stack with fewer keystrokes. It's a great alternative to using cd every time you want to move to a new directory. Especially for short trips where you need to compile something, then jump to the build directory and run the results. Give it a try and see how much time and effort you can save!

More from Lofty