Tuesday, May 17, 2011

cd -, a bash command that takes you back to the previous directory

cd -, a very handy bash command that takes you back to the previous directory.

For example, i am in /var/www/. i first cd drupal/, then cd ../joomla. Now, i want to go back to /var/www/drupal, i can simply type cd -, and if i want to go back to /var/www/joomla again, i can input cd - again and it will take me back to joomla folder.

We can use this command to quickly switch back and forth between two directories.

Well but what happen when you just login the system and enter cd -? Well the possibility is you will see some information like OLDPWD is not set. So let's do an interesting test, if we change OLDPWD manually:
export OLDPWD='/home/henry'
and then we type cd -, and as expected, we are now in /home/henry folder.

No comments: