How to use the Linux pushd command

With Linux pushd you store directory paths, change between directories and add them to your stack. Although there are other Linux commands for these operations, pushd has some distinct advantages.

What is the Linux pushd command?

pushd is one of the commands in Linux that’s not as well-known as other commands for daily working with Linux. That’s a pity, because the command has a lot of potential and can facilitate the use of the command line. The main task of Linux pushd is to save the current directory path and move it to the top of your stack. This makes it easier for you to return to a particular working directory.

pushd is standard in common Linux distributions like Debian or Ubuntu, and other operating systems.

How does Linux pushd work?

When running the pushd command to create a specific directory for it as a parameter, it automatically becomes your current working directory. At the same time, the name and path of the selected directory are moved to the top of your stack. If you run pushd without any further specifications, the directory at the top of your stack becomes your current working directory; the top two directories thus swap positions. Although you could alternatively use the Linux-cd command, it requires several executions for more extensive operations, whereas pushd goes directly to the target.

What is the syntax of the pushd command?

The syntax of pushd always looks the same:

$ pushd [Options] [Directory]
bash

If you don’t specify a directory, the directory at the top of your stack is automatically used.

What options does Linux pushd offer?

There’s only one other option for the pushd command besides the standard ‘-h’ and ‘-v’. With ‘-n’ you can add a new directory to the stack without affecting your current directory. This means that the new directory moves to the second position in the stack and your current directory remains at the top.

Examples of the Linux pushd command

To help you understand how the pushd command works and take a closer look at its advantages, here are some examples.

$ pushd ~/example
bash

Use this command to make the directory ‘example’ your new working directory.

$ pushd -n ~/images
bash

If you execute the command like this, the directory Images will be inserted in second place in your stack. However, you continue to work in your current directory.

Was this article helpful?
Page top