How to go back n levels of directories from the command line?

This is shell specific. In a pure POSIX shell, you can't without typing cd /tmp again. In csh, tcsh, bash or zsh, you can use pushd instead of cd to change directories and then popd +1 to cd to the first directory you pushed onto the stack.


Read this article from LinuxGazette on adding a history to cd with a few simple scripts. It's quite usable.

If you want to be fancy, then there is a program called autojump (homepage here) which keeps history of the directories visited, provides shortcuts and generally supports "intelligent" cd.

autojump is a faster way to navigate your filesystem. It works by maintaining a database of the directories you use the most from the command line. The jumpstat command shows you the current contents of the database. You need to work a little bit before the database becomes usable. Once your database is reasonably complete, you can “jump” to a commonly "cd"ed directory by typing: j dirspec