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

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

By sk
Published: Last Updated on 2156 Views

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.

Let us see some practical examples to understand better.

Use Pushd, Popd And Dirs Commands For Faster CLI Navigation

Pushd, popd ,and dirs commands 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.

Display present working directory using pwd command

Display present working directory using pwd command

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

Or,

$ 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
Change to a specific directory using pushd command

Change to a specific directory using pushd command

To know where you are now, just type:

$ dirs

Sample output:

/tmp/test3 /tmp /tmp
Show current directory stack using dirs command

Show current directory stack using dirs command

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
Go to previous working directory using popd command

Go to previous working directory using popd command

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
Go to a specific directory using pushd command

Go to a specific directory using pushd command

Let us go deep in the stack.

$ pushd /tmp/test10

Sample output:

/tmp/test10 /tmp/test8 /tmp /tmp
Go to a specific directory in the directory stack using pushd

Go to a specific directory in the directory stack using pushd

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 you don't need to use cd command. We can quickly move to the test8 directory by running the popd command:

$ popd

Sample output:

/tmp/test8 /tmp /tmp
Move to previously working directory with popd command

Move to previously working directory with popd command

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
Move to the last visited directory with popd

Move to the last visited directory with popd

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.

A similar set of commands are available for faster CLI navigation in Fish shell. For more details, refer the following guide:

You know now how to effectively navigate between directories without using pushd, popd and dirs commands. 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.

Also read:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

2 comments

Rizwan Mohammed December 31, 2016 - 5:36 pm

Thank you for you effort

Reply
Jane Doe March 14, 2017 - 3:21 am

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

Reply

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