Stacking Directories With β€œpushd” and β€œpopd” Commands for Easy Navigation

Linux TLDR
Last Updated:
Reading time: 3 minutes

The cd command is a very popular tool for navigating through multiple directory paths in the Linux filesystem, but there is one problem with that, what?

A directory with a short path can be easily navigated, but a directory with a long path might become troubling while going back and forth multiple times.

It’s true, you can create an alias for directories with a long path, but when you interact with multiple directories nested deeply inside your filesystem, your only option is the cd command.

But it is not anymore after you read this article and discover the most amazing Linux tools: β€œpushd” and β€œpopdβ€œ.

Tutorial Details

DescriptionPushd and Popd
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitespushd, popd, dirs
Internet RequiredNo

What is the Pushd, Popd (and Dirs) Commands in Linux?

The pushd and popd commands are ways to efficiently navigate between different directory paths by stacking them in memory and popping them out when they are required using the index number.

It works on the principle of β€œLIFO” (last in, first out), which means to push an item into the stack and pop an item out of the stack.

Surely, you are not going to stack a single directory path in a stack (otherwise, you wouldn’t require this tool), so you can use the dirs command to list the stacked path in memory to help you figure out their index number.

All commands and their definitions are listed below:

  • pushd: Place a directory on a stack so it can be accessed later.
  • popd: Remove a directory placed on the directory stack via the pushd command.
  • dirs: Displays or manipulates directory paths in the stack.

With that, let’s check out their usage in a practical way.

How Pushd and Popd Commands Work in Linux

Let’s start with the pushd command that lets you add directory paths to the stack (list or history) and later lets you easily navigate between them (from list or history).

The following is an example of the pushd command.

$ pushd ~/Documents/
$ pushd ~/Downloads/
$ pushd /var/www/html/
$ pushd ~/Desktop/

Output:

Stacking directory paths

As you can see above, when you add a directory path to the stack, it will redirect you there and echo the already-existing and pushed path. These stacked paths are also temporary and will be gone when you restart your system or terminal session.

One more thing you should note is that whenever you add a directory to the stack, its index starts in reverse order.

  • Index 0 = β€œ~/Desktop/β€œ
  • Index 1 = β€œ/var/www/htmlβ€œ
  • Index 2 = β€œ~/Downloads/β€œ
  • Index 3 = β€œ~/Documents/β€œ

If you add another directory path to the stack, the new stacked directory path index will be β€œ0β€œ, and the rest of the stacked directory index will be increased by one.

Currently, the stack echoes their path in the output so you can figure out their index number, but while working and executing a series of commands, these echoes will fade output in between, making it a little more difficult for you to recall the index position of paths.

Or, list the stacked directory path with one entry per line and its index position in the stack using the β€œ-v” flag.

$ dirs

Output:

Listing stacked directory paths

Or, list the stacked directory path with one entry per line with its position in the stack using the β€œ-v” flag.

$ dirs -v

Output:

Listing stacked directory paths per entry in one line

So, from earlier, you know that each stacked entry is assigned an index number (from 0 to nth), so to switch between different directories like β€œ/var/www/htmlβ€œ, you use the index number (that is, 1) in the form of β€œpushd +n” format.

$ pushd +1

Output:

Navigating into the stack directory using the index number

Note that after switching to a stacked directory path, the stack index number will change, so the path will be echoed when you switch, or you can again use the dirs command, as shown.

$ dirs -v

Output:

Checking the modified directory path index number

For example, if you want to switch to the β€œ~/Documents” path, then use 2 as the index number with the pushd command.

$ pushd +2

Output:

Switching to another directory path using the pushd command

Again, the index numbers for the directory paths will change, and it might look confusing for a beginner, but surely you will get used to it after two or three tries.

Removing Item From Stack

Every directory path you put/add to the directory stack might become unnecessary afterward, so at that time you can easily remove it using its index number, except this time you will use the popd command.

For example, the following is the list of existing stack directory paths, and I removed the β€œ/var/www/html” using its index number 3 in β€œpopd +n” format.

$ popd +3
$ dirs -v

Output:

Removing an item from the stack

Or use the β€œdirs -c” command to remove all the stacked directory paths.

$ dirs -c

Output:

Clearing the stacked directory paths

That was all about the stacking directory paths.

If you have any doubts or questions regarding this topic, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.

2 thoughts on β€œStacking Directories With β€œpushd” and β€œpopd” Commands for Easy Navigation”