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.
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
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 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
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.
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:
2 comments
Thank you for you effort
Didn’t know about these commands.
Thank you sir!