Linux Commands

Linux PUSHD and POPD

You must be familiar with the “push” and “pop” terminologies if you have ever worked with stacks. These two are the most commonly used operations while working with stacks. The former one is used to add something to the stack; whereas the latter one is used for extracting the last added element out of the stack. Similarly, there is also a directory stack in Linux which is meant for holding the directories of your system. Just like the regular stack, you can push and pop your directories while using the directory stack. For that, the PUSHD and POPD commands are used which we are going to throw light on in this article.

Usage of PUSHD and POPD in Linux

The PUSHD command in Linux is simply used to push a directory into the directory stack; whereas the POPD command pops out the recently pushed directory from the directory stack. This directory stack acts as a last in first out (LIFO) queue. To understand this, you need to go through the following steps:

Step # 1: Checking the Directory Stack

First, we will try to list the current contents of our directory stack with the following command:

$ dirs –l –v

You can see the current status of our directory stack in the image shown below. From here, you can clearly visualize that at the moment, we only have a single entry in our directory stack.

Step # 2: Using the PUSHD Command

Now, we will try to push a directory inside our directory stack with the following command:

$ pushd ~/Desktop

This command will push the Desktop directory at the top of our directory stack. Not only this but it will also make the specified directory our current working directory. The output shown in the image below verifies this:

Step # 3: Checking the Directory Stack Again

We will check the directory stack again with the following command to check if the directory specified in the second step has successfully been pushed to the directory stack or not:

$ dirs –l –v

The status of our directory stack now is shown in the image below. You can visualize from this image that the said directory has successfully been pushed to the top of our directory stack.

Step # 4: Using the POPD Command

Now, we will attempt to pop the recently pushed directory from our directory stack using the following command:

$ popd

A successful execution of this command will produce the output shown in the image below on the terminal:

Step # 5: Checking the Directory Stack Yet Again

Finally, for verifying if the recently pushed directory has successfully been popped from our directory stack or not, we will run the following command:

$ dirs –l –v

You can verify from the image shown below that the directory stack has been popped successfully or if the recently pushed directory has been successfully popped out of the directory stack.

Conclusion

This article was designed to give you a quick hint on the working of the PUSHD and POPD commands in Linux. We elaborated on the working of these commands by using a very simple step-wise example. After going through this example, you will easily be able to use the PUSHD and POPD commands in Linux.

Similar Posts