Home Command line utilities How To Use Pushd, Popd And Dirs Commands For Faster CLI Navigation
Use Pushd, Popd And Dirs Commands For Faster CLI Navigation In Linux

How To Use Pushd, Popd And Dirs Commands For Faster CLI Navigation

By sk

We all know that we use cd command to move from one directory to another. To return back to the previous directory, we use "cd .." or "cd <location_of_previous_directory>" commands. This is how I mostly navigate between directories until I found the triplet commands, namely pushd, popd, and dirs. These three commands provides a way faster navigation between directories. Unlike cd command, pushd and popd commands are used to manage a stack of directories. Just enter into a directory and do something you want to do, and "pop" back to the previous directory quickly without having to type the long path name. dirs command is used to show the current directory stack, just like "ls" command. These trio commands are extremely useful when you're working in a deep directory structure and scripts.

Still confused? No worries! I am going to explain these commands in layman terms with some practical examples.

Use Pushd, Popd And Dirs Commands For Faster CLI Navigation

Pushd, popd ,and dirs commands are comes pre-installed, so let us not bother about the installation and go ahead to see how to use them in real time.

I am in /tmp directory now.

I am going to create ten directories, namely test1, test2, .... test10 in /tmp directory.

As may already know, We can easily create multiple directories at once using mkdir command as shown below.

$ mkdir test1 test2 test3 test4 test5 test6 test7 test8 test9 test10


$ mkdir test{1,2,3,4,5,6,7,8,9,10}

Now, let us move to test3 directory. To do so, just type:

$ pushd test3

To know where you are now, just type:

$ dirs

Sample output:

/tmp/test3 /tmp /tmp

As you see in the above output, dirs command shows we have two directories in the stack now. Do anything you wish to do in this directory. Once done, you can go back to your previous working directory using command:

$ popd

No need to mention the full path of previous directory. If you use cd command, you should type "cd .." or "cd <full_path_name>" to go back to the /tmp directory. But, using popd command we can instantly move back to the previous working directory. It's simple as that.

Let us go again to test8 directory. To do so, run:

$ pushd test8

Sample output:

/tmp/test8 /tmp /tmp

Let us go deep in the stack.

$ pushd /tmp/test10

Sample output:

/tmp/test10 /tmp/test8 /tmp /tmp

We're now in test10 directory, and we have totally 3 directories (test10, test8 and tmp) in our stack. Did you notice the direction? Each new directory is getting added to the left. When we start poping directories off, they will come from the left as well.

Now, if you want to move to the previous working directory i.e test8 using cd command, the command would be like below.

$ cd /tmp/test8

But it is not necessary though. We can do it more quickly by running the popd command.

$ popd

Sample output:

/tmp/test8 /tmp /tmp

As you see in the above output, we moved to the previous working directory without having to type full path (i.e /tmp/test8).

Now, let us pop again?

$ popd

Sample output:

/tmp /tmp

Finally, We came back to the directory where we started.

In this example, I have used just ten directories. So, It may seem it is no big deal. Think about twenty or more directories? Would you type "cd <path_name>" or "cd .." each time to move between directories? Nope. It would be time consuming. Just use pushd command to change to any directory in the stack and move back to your previous working directory using popd command. Also, you can use dirs command to show the current directory stack at any time. You can add a series of paths onto your stack and then navigate to them in the reverse order. This will save you lot of time when you are navigating around stack of directories.

Also read:

You know now how to effectively navigate between directories without using cd command. These commands comes in handy when you're working with large directory stack. You can quickly move back and forth between N number of directories. These commands are also much useful working with scripts too.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like


Rizwan Mohammed December 31, 2016 - 5:36 pm

Thank you for you effort

Jane Doe March 14, 2017 - 3:21 am

Didn’t know about these commands.
Thank you sir!


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More