One practical use that I have found is to toggle between directories using pushd and popd.
Suppose we have two directories dir1 and dir2 and I need to toggle between them continuously for some xyz reason.
Currently I am in somedir which has two directories dir1
and dir2
:
alcohol@alcohol-machine:~/somedir$ ls
dir1 dir2
alcohol@alcohol-machine:~/somedir$
Now I switch to dir1
alcohol@alcohol-machine:~/somedir$ cd dir1
alcohol@alcohol-machine:~/somedir/dir1$ pwd
/home/alcohol/somedir/dir1
Now I will add dir1
to stack and switch to dir2
alcohol@alcohol-machine:~/somedir/dir1$ pushd /home/alcohol/somedir/dir2
~/somedir/dir2 ~/somedir/dir1
alcohol@alcohol-machine:~/somedir/dir2$
As you can see, I now have dir2
and dir1
on stack and I am currently into dir2
.
Now to switch back to dir1
, I will run pushd || popd
.
alcohol@alcohol-machine:~/somedir/dir2$ pushd || popd
~/somedir/dir1 ~/somedir/dir2
alcohol@alcohol-machine:~/somedir/dir1$
Voila, I am into dir1 and I have dir1 and dir2 on stack.
To switch back to dir2 again run pushd || popd
alcohol@alcohol-machine:~/somedir/dir2$ pushd || popd
~/somedir/dir1 ~/somedir/dir2
alcohol@alcohol-machine:~/somedir/dir1$
This a simple way to toggle between directories. Now, you might what to know, why I need to toggle between directories? Well one use case is, when I work on web application, I have my make file in one directory and my log files in another directory. Often when debugging the application, I need to switch between the log directory, to check the latest logs and then switch back to the make directory, to make some changes and build the application.