When working in the command line, one of the things you will most likely do is to switch back and forth between working directories.

You might be doing some work in the directory/home/user/files/ and might also at the same time be working  on /var/www/public_html/.

If you are just working between two directories, then it’s easy to just use the command

$cd -

That would allow you move back to the directory which you were last in. You can use it to toggle between the last two directories you have worked in. See example below

user@comuter:/home/user/files$ cd /var/www/public_html
user@comuter:/var/www/public_html$ cd -
user@comuter:/home/user/files$ cd -

There’s another way to switch between working directories, and that’s using pushd and popd.

Both commands are use to work with the directory stack.
pushd is used to save a directory location into the stack while

popd is used to return to the directory location stored at the top of the stack.

The directory stack is a FILO (First In Last Out), meaning the last directory you put into the stack will be the first to be “popped” out.

To call pushd it’s: $pushd <directory location>
and to call popd it’s simply: $popd

user@comuter:/home/user/files$ pushd .
user@comuter:/home/user/files$ pushd /var/www/public_html
user@comuter:/home/user/files$ pushd /var/log
user@comuter:/home/user/files$ popd
user@comuter:/var/log$ popd
user@comuter:/var/www/public_html$ popd

pushd and popd work in Command Line Editors, such as Unix, Linux, Solaris and DOS.

So the next time you’re working in a command line editor, remember to be pushding and popding your directories to work more efficiently

Edwin is the founder and editor of Little Handy Tips and Wollongong Fitness. He is also the developer for the Google Custom Search WordPress plugin and Custom About Author WordPress plugin. Find out more about him here.