Pushd And Popd – Easy Way To Navigate Directories Faster In Linux
We had written few articles on this title, which are allow us to quickly go back to the given parent directory.
We normally use
cd /go/to/the/path to go to the given directory.
cd .. to come back one parent directory and, use
cd to go to parent directory that 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 would like you to check the following URLs.
- Quickly Go Back To A Specific Parent Directory Using bd Command In Linux
- UP Shell Script – Quickly Navigate To A Specific Parent Directory In Linux
What Is pushd And popd?
pushd and popd are shell builtin commands. It uses LIFO (Last in, First out) method.
pushd command is used to push the current directory information into a stack and
popd removes a directory from the top of the stack.
dirs command will show you the directory stack information.
These commands are allowed 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
To do so, just run the following command.
# pwd /root # pushd /usr/share/icons/Paper/ /usr/share/icons/Paper ~ [email protected]:/usr/share/icons/Paper# # dirs /usr/share/icons/Paper ~
The above output clearly showing that I’m in
If you want to quickly go back to
/root then simply run the following command.
[email protected]:/usr/share/icons/Paper# popd ~ [email protected]:~# # pwd /root
Yup, I came to
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 confuse, 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
[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
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 [email protected]:/opt#
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 [email protected]:/usr/share/icons/Paper#
It will move (cut & past) 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 to till 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.
[email protected]:/home/daygeek# popd /var/log ~ [email protected]:/var/log#