pushd command in linux 1

How to Use pushd Command in Linux

The pushd command in Linux or any Unix-based system is a great command-line utility to work with a directory stack. With this command, you can add the working directory to the stack and assign it to the top position.

As the name suggests, the directory stack refers to the list of directories you’ve operated or at least navigated to. Keep in mind that the current working directory always sits at the top of the stack.

The current working directory is the directory/folder that you’re currently using. To view your system’s directory stack, you can use the dirs command. Now, when you wish to save the current directory and add it to the stack, you’ll invoke the pushd command.

pushd Command in Linux: Syntax

The basic structure (syntax) of the pushd command in Linux looks something like this:

pushd command in Linux

Working of pushd Command 

Before talking about the uses of the pushd command in Linux, let us quickly walk through its working mechanism. The pushd command, when invoked, starts by saving the current working directory to the very top of the stack. It then navigates to the directory provided. 

It is crucial to keep in mind that the current working directory always takes the top spot in the stack. When an alteration is initiated, the new current directory replaces the previous one in the stack. Still, it is not saved until the pushd command is employed.

In case you fail to run the pushd command and use the cd command instead to navigate forward, the item at the top of the stack will get lost.

Using pushd Command in Linux

As already discussed, the pushd command in Linux helps a user work with the directory stack and save the current working directory to the top. For instance, if you’re willing to assign the /var/www directory to the top of the stack, run the pushd command as:

$ pushd /var/www
using pushd command

On successful completion, your system will start displaying the desired directory on the top of the stack. It will be followed by the tilde symbol ~ which defines the home directory.


/var/www ~

Suppressing the Directory Change

Apart from assigning the current working directory to the top of the stack, the pushd command in Linux can help suppress the alteration as well. You’ll need to pass the -n flag for this.

If you’re willing to add a directory /var/www but don’t want to bring any changes to the order of the stack, run the pushd command as:

~$ pushd -n /var/www
suppressing directory change

It will add the /var/www directory while keeping ~ at the top of the stack. From the output, you can see it;/var/www gets added at the second position from the left.

suppressing directory change in Linux

pushd Command: Options and Arguments

The push command in Linux accepts one option and three arguments. While suppressing the order alteration is the sole option, some arguments assist in navigating across the directory stack.


-n: It helps suppress the usual change of directory when new directories are added to the stack. It ensures that only the stack is manipulated. 


+N: Using this flag will rotate the stack in a manner that the Nth directory (when counted from the left of the list, as displayed by dirs starting with zero) appears at the top. 

-N: It simply does the opposite to +N. -N flag rotates the entire stack so that the Nth directory (when counted from the right of the list as displayed by dirs, starting with zero) appears at the top.   

dir: It adds DIR to the top of the directory stack, making it the current working directory.

How to Work with Arguments

The +N and -N arguments associated with the pushd command are great for navigating to the desired directory and bringing it to the stack’s top. Where N denotes the number you want to hover over to, the + and - sign respectively indicates that the counting is done from the left and right. 

To instruct your system, print the list of available directories, launch the Terminal, and run the following command:

$ dirs -l -v
list directories

For instance, you’ve got an index list that looks like this:

0 /opt
2 /usr/local1
3 /var/www
4 /usr local2
5 /home/arbas19

Now, if you want to change to the /usr/local1 and bring it to the very top position in the stack, you can use either of the following:

pushd +2 or pushd -4

When counted from the top to bottom, the index of the desired directory is 2; hence you can run the command as:

$ pushd +2

In case you’re counting from the bottom, the index will change to -4. Thus, for navigating and setting /usr/local1 to the top of the stack, you’ll need to run the following command:

$ pushd -4

When the pushd command is invoked without any argument, it toggles the position of the first two directories and assigns the new one as the current working directory. It is similar to how the cd command in Linux works.

pushd Command Vs. popd Command

Although the pushd command and the popd commands are shell builtins, both differ from one shell to the other. While the former helps add a directory to the stack, the latter helps in the removal of the same.

And with that, we’re done discussing the pushd command in Linux. In most situations, we tend to employ the cd command for navigating from one directory to the next; however, the same can be efficiently concluded with the pushd command.

In this article, we’ve looked at how the pushd command works and talked about the options and arguments associated.

If this guide helped you, please share it.

Leave a Reply
Related Posts