pushd and popd are two commands that are absolutely worth their learning time if you are in the business of using the command line on a regular basis on linux. One of the biggest hindrances of using a command line for file manipulation and such would be navigation between folders. Sometimes its might be a pain to navigate between two folders, even using TABs for auto completion. This is where pushd and popd comes in to the picture.
What this does, in short, is, create a stack containing the paths that you have traversed using pushd. You can revisit these paths using popd. Remember that this is a stack in all aspects.
A short intro on how a stack works, if you haven’t come across it already. Skip this paragraph if you know what a stack is. Stack is a data structure that is like a stack of books or a stack of biscuits or best, pringles. You can stack one item on top of each other, but you can only take the item on top. Logical right?. You stack an item using push. As in, pushing a potato chip into a pringles case. And you take one out using pop. Of course, you can only take the one on the top, a position called the ‘top of stack’.
Suppose you are in your home folder (~). You move to another directory by typing,
This will give you an output with two directory listings. The first one is the directory that you have navigated ‘to’. The second one is the directory that you just navigated ‘from’.
You can navigate to another folder with something like say,
Now you will have the following output
~/Downloads ~/Documents/work-docs ~
What this shows is a history of your past navigations, or a ‘stack’ of your previous directories. The top of stack being the left most. Now to go back one step to
~/Documents/work-docs/, all you have to do is type,
and it will give you the following output
That would be a quick intro of pushd and popd. Have a good time navigating through your directories.