So, you are stuck in a shell, debugging some random problem. And you have to navigate between two directories, iterating through a debug session - say between a configuration directory like /etc/apache/sites-available and a log directory like /var/log/apache.

Any sane person would open another shell, but in this case you can’t. Or just couldn´t be bothered. So you are stuck in that shell typing in cd /var/log/apache, checking the log-file, then typing in cd /etc/apache/sites-available again and so forth.

Turns out bash supports several ways to do this more efficiently.

cd -

cd - will instruct the shell to change directory to the previous working directory. Repeating the command will instruct the shell to change directory to the previous working directory (which handily turns out to be the original working directory):

larso@goethe:/etc/apache2/sites-available$ cd /var/log/apache2/
larso@goethe:/var/log/apache2$ cd -
larso@goethe:/etc/apache2/sites-available$ cd -

cd - works well when navigating between two directories. But what if I want to navigate through three or more directories?

pushd and popd

pushd will push the current directory on a stack, then navigate to the directory given as an argument:

larso@goethe:~$ pushd /var/tmp
/var/tmp ~

To navigate back, simply use popd:

larso@goethe:/var/tmp$ popd

Since pushd works as a stack, you can push several directories on it, then pop out through those directories again:

# pushd
larso@goethe:/etc/apache2/sites-available$ pushd /var/log/apache2/
/var/log/apache2 /etc/apache2/sites-available

larso@goethe:/var/log/apache2$ pushd /var/www
/var/www /var/log/apache2 /etc/apache2/sites-available

larso@goethe:/var/www$ pushd /etc/apache2/sites-available/
/etc/apache2/sites-available /var/www /var/log/apache2 /etc/apache2/sites-available

# popd
larso@goethe:/etc/apache2/sites-available$ popd
/var/www /var/log/apache2 /etc/apache2/sites-available

larso@goethe:/var/www$ popd
/var/log/apache2 /etc/apache2/sites-available

larso@goethe:/var/log/apache2$ popd

larso@goethe:/etc/apache2/sites-available$ popd
-bash: popd: directory stack empty


Another approach to quickly navigate through several directories is by using the CDPATH variable. This variable defines a colon separated search path for your shell. Set your CDPATH variable to one level above the directories you want to jump between, then simply use cd with a non-absolute argument:

larso@goethe:~$ export CDPATH="/etc/apache2:/var/log:/var"
larso@goethe:~$ cd sites-available
larso@goethe:/etc/apache2/sites-available$ cd apache2
larso@goethe:/var/log/apache2$ cd www

Unfortunately, tab-completion stops working when changing directories using the CDPATH variable functionality.


Bash has another trick up its sleeve - it can change minor mistypes when changing directory. According to the manual, the errors checked for are transposed characters, a missing character, and one character too many.

Use the shoptcommand to turn on (or off) this feature:

larso@goethe:~$ shopt cdspell
cdspell        	off
larso@goethe:~$ shopt -s cdspell
larso@goethe:~$ shopt cdspell
cdspell        	on
larso@goethe:~$ shopt -u cdspell
larso@goethe:~$ shopt cdspell
cdspell        	off

Let´s test it:

larso@goethe:/etc/apache2$ cd site-available
larso@goethe:/etc/apache2/sites-available$ cd /Tmp