Pushd & Popd : Easy way to navigate between directories faster in Linux

We had written few articles on this title, which allows us to quickly go back to the given parent directory.

We normally use cd /go/to/the/path to go to the given directory.

we use cd .. to come back one level parent directory and, use cd to go to home directory that’s where we started.

But we didn’t get a chance to write an article for vice versa.

Today we are going to discuss about this topic. These commands were pre-installed in most of the systems so, you need to install it.

Just learn, how to use these commands to complete your activity much faster when you are working on directory structure in most of the time.

If you are looking for other alternatives, I recommend  you to check the following URLs.

What Is pushd And popd?

pushd and popd are shell builtin commands. It uses LIFO (Last in, First out) method.

The pushd command is used to push the current directory information into a stack and popd removes a directory from the top of the stack.

The dirs command will show you the directory stack information.

These commands allows us to navigate between directories faster.

How to use pushd and popd for faster navigation?

Now, you can see the magic.

Now, I’m in /root directory and I’m planning to go /root/usr/share/icon/Paper directory.

To do so, just run the following command.

# pwd

# pushd /usr/share/icons/Paper/
/usr/share/icons/Paper ~
# dirs
/usr/share/icons/Paper ~

The above output clearly showing that I’m in /root/usr/share/icon/paper directory.

If you want to quickly go back to /root then simply run the following command.

root@Ubuntu18:/usr/share/icons/Paper# popd

# pwd

Yup, I came back to /root directory.

How to use pushd and popd for faster navigation using Index Number?

It seems you were confused, and you will be thinking where are these data has been stored?

If so, don’t get confused, we will tell you what is behind this. To clarify this, we are going to navigate few directories and show you the output.

# pushd /opt
/opt ~

# pushd /home/daygeek
/home/daygeek /opt ~

# pushd /usr/share/icons/Paper
/usr/share/icons/Paper /home/daygeek /opt ~

# pushd /var/log
/var/log /usr/share/icons/Paper /home/daygeek /opt ~

List of the commands that I performed will be stored in the directory stock. To see the directory stock information, run the following command.

# dirs
/var/log /usr/share/icons/Paper /home/daygeek /opt ~

For details, use -v switch with dirs command.

# dirs -v
 0  /var/log
 1  /usr/share/icons/Paper
 2  /home/daygeek
 3  /opt
 4  ~

As we told in the beginning of the article, it uses FIFO methods so, the index number should be assigned in reverse order.

So, the output of the directory stack actually looks like below. Just assign the index number starting zero from Left to Right.

[Index 0] : /var/log
[Index 1] : /usr/share/icons/Paper
[Index 2] : /home/daygeek
[Index 3] : /opt
[Index 4] : ~

To go to the specific directory in directory stock, we have to use the pushd command with index number like pushd +N or pushd -N.

If i would like to go /opt directory then i have to run the following command.

# pushd +3
/opt ~ /var/log /usr/share/icons/Paper /home/daygeek

Once you ran the above command then it will rearrange the index and you have to use accordingly.

To go to /usr/share/icons/Paper then i have to use the following command.

# pushd +3
/usr/share/icons/Paper /home/daygeek /opt ~ /var/log

It will move (cut & paste) all the directories to the end without changing the format, which is available in front of the given directory.

Based on the above example. It will move all the available directories in front of the /usr/share/icons/Paper directory to end.

It splits the directory stack with two parts Part-A & Part-B based on the request. The requested directory should be Part-A, it will go till the end of the stack.

The remaining directories should be Part-B and it’s moved to after Part-A. Also, it doesn’t remove any directories from the stack until you use popd.

If you would like to remove /opt from the directory stack. Run the following command.

# dirs
/home/daygeek /opt /var/log ~

# popd +1
/home/daygeek /var/log ~

If you would like to go back to /var/log. Just run the following command.

root@Ubuntu18:/home/daygeek# popd
/var/log ~

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *