With Linux pushd you store directory paths, change between di­rec­to­ries and add them to your stack. Although there are other Linux commands for these op­er­a­tions, pushd has some distinct ad­van­tages.

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 fa­cil­i­tate 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 par­tic­u­lar working directory.

pushd is standard in common Linux dis­tri­b­u­tions 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 au­to­mat­i­cal­ly 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 spec­i­fi­ca­tions, the top two di­rec­to­ries of your stack swap positions: the directory that was pre­vi­ous­ly in second position becomes your current working directory. Although you could al­ter­na­tive­ly use the Linux-cd command, it requires several ex­e­cu­tions for more extensive op­er­a­tions, 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, pushd swaps the top two entries of your stack and changes to the directory that was pre­vi­ous­ly in second position.

What options does Linux pushd offer?

There’s only one option for the pushd command. 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 un­der­stand how the pushd command works and take a closer look at its ad­van­tages, 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.

Reviewer

Go to Main Menu