Moving Again

So far you have already used the cd command to change your current working directory and the pwd command to find out what your current working directory is.  After you work with the command line for a while, you'll find yourself changing directories constantly.  In order to make this easier, Bash provides a "directory stack" that you can use to quickly move around directories where you are doing some work.  (We'll show some examples in a moment that help explain the idea of a "stack.") You have the following commands at your disposal:

Command Purpose
pushd dir
Push dir to the top of the stack and change the current working directory to it

pushd

pushd +N

Switch the two top entries, changing back to the most recent directory (when entered without +N)

Rotate N items on the stack, changing to the directory that comes out on top of the stack (when entered with +N)

popd

Remove the directory from the top of the stack and change the current working directory to the one at the top of the stack

dirs

Display the directory stack

If you need to have more of a visual aid to understanding a "stack", the simplest way is to imagine the "stack" as a pile of papers on your desk, you "push" new pieces of paper onto the top of the "stack" and you "pop" the top most piece of paper off the "stack". Both methods work on the principle of LIFO (last in first out).

You can play around with these commands to understand how they work.  For example the following table provides a list of commands, their effect on the current working directory and their effect on the stack.

Command

Current working directory after command

Stack after command
cd
~
~
pushd /
/

/

~

pushd /usr/bin
/usr/bin

/usr/bin

/

~

pushd +1
/

/

~

/usr/bin

pushd +1
~

~

/usr/bin

/

popd
/usr/bin

/usr/bin

/

pushd +1
/

/

/usr/bin

popd
/usr/bin
/usr/bin