Directory Management using cd vs pushd

Kishan Maurya
2 min readDec 7, 2019

The most common way to change directory from the command line is cd
How do we change the directory?

change directory using cd command

The above image shows the changing directory in a sequential manner like going one step down each time.

let suppose from my current directory( /Users/kishankumarmaury ),
I go to (/Downloads/GOJEK) and from there I go to (/Development/Node)

kishankumarmaurya@Manavs-MacBook-Pro ~ % pwd
/Users/kishankumarmaurya
kishankumarmaurya@Manavs-MacBook-Pro ~ % cd ~/Downloads/GOJEK
kishankumarmaurya@Manavs-MacBook-Pro GOJEK % pwd
/Users/kishankumarmaurya/Downloads/GOJEK
kishankumarmaurya@Manavs-MacBook-Pro GOJEK % cd ~/Development/Node
kishankumarmaurya@Manavs-MacBook-Pro NodeLearning % pwd
/Users/kishankumarmaurya/Development/Node

so present working directory will be

/Users/kishankumarmaurya/Development/Node

Now if I want to go back to GOJEK folder then I have to use cd command with full path.

Painful thing is that we have to remember the path of the previous directory.

Thanks to pushd and popd command from saving us to remember the directory path.

How pushd and popd works?

pushd command is used to save the current directory into a stack and move to a new directory.
popd can be used to return back to the previous directory that is on top of the stack.

pushd and popd work according to the “LIFO” (last in, first out) principle

Example of pushd :

Stack Information~/Development/Node/NodeLearning 
~/Development/Node
~/Downloads/GOJEK/DRIVER
~/Downloads/GOJEK
~

Now I want to make ( /Development/Node ) as on Top
use

pushd +1After this command stack will look like this
~/Development/Node
~/Downloads/GOJEK/DRIVER
~/Downloads/GOJEK
~
~/Development/Node/NodeLearning
pushd +2 //will move /Downloads/GOJEK at top~/Downloads/GOJEK
~
~/Development/Node/NodeLearning
~/Development/Node
~/Downloads/GOJEK/DRIVER

Example of popd :

~/Development/Node/NodeLearning
~/Development/Node
~/Downloads/GOJEK/DRIVER
~/Downloads/GOJEK
~
popd~/Development/Node
~/Downloads/GOJEK/DRIVER
~/Downloads/GOJEK
~
--------------------------------------------------------------------
Now let say we have stack~/Development/Node
~/Downloads/GOJEK/DRIVER
~/Downloads/GOJEK
~
and we hit command popd +1 then stack state will be
~/Development/Node
~/Downloads/GOJEK
~
--------------------------------------------------------------------

To know the stack of directories use dirs and to clear entire stack use dirs -c

Stack Information

Thanks for reading. Soon I will post more articles on android, core java.
till then happy learning… keep reading… :)

You could check out my other interesting topics here.

--

--