Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to go back to previous directory we were in using bash,tcsh without using pushd/popd ? I'd like to type something like "back" and got returned to the previous directory I was in.


"cd -" works, but only for current and previous directories. Is there anyway I can go back to the previous previous previous directory like how you can go back in the web browser?


share|improve this question
As noted below, you can do so using "pushd" and "popd". – blueyed Mar 5 '10 at 3:02
Just a side note "cd --" goes to the user default direcotry (/home/username) – sdaffa23fdsf Apr 8 '12 at 22:49
up vote 226 down vote accepted

cd - (goes back to previous directory)

If you want to be able to go to the other previous directories, this is not possible out of the box. But check this script and instructions:

History of visited directories in BASH

The cd command works as usual. The new feature is the history of the last 10 directories and the cd command expanded to display and access it. cd -- (or simply pressing ctrl+w) shows the history. In front of every directory name you see a number. cd -num with the number you want jumps to the corresponding directory from the history.

share|improve this answer
also pushd and popd might be useful – lorenzog Feb 25 '10 at 8:55
@lorenzog : lydonchandra, in his question, said "without using pushd/popd" – Snark Feb 25 '10 at 9:19
whops. missed it. thanks – lorenzog Feb 25 '10 at 9:50
cd -- seems to go back 2 directories – ogc-nick May 21 '14 at 17:07
@ogc-nick for using this cd -- in menu-like manner, you should use the mentioned script – Ram Feb 11 at 6:13

You can also use variable cd $OLDPWD. This can be also used in shell scripts.

share|improve this answer
$OLDPWD maintains the last directory you came from which is good for scripts. I use $OLDPWD with cp command a lot. E.g cp -v $OLDPWD/file . – Neil Wightman Jan 9 '15 at 9:12
@NeilWightman Great little gem. Thank you! – joshperry Jun 8 at 16:41

I think cd .. might help. If you do a ls -a in any directory you would see that there are two entries: one named "." and another named ".."; the single dot is reference to the directory you are already in, while the double is the previous directory in the path.

share|improve this answer
.. is not the previous directory, it's just the parent directory. – Oliver Salzburg Dec 10 '12 at 10:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .