374

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.

Edit:

"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?

Regards

Lydon Ch
  • 5,789
  • 8
  • 32
  • 35
  • 1
    As noted below, you can do so using "pushd" and "popd". – blueyed Mar 05 '10 at 03:02
  • 8
    Just a side note "cd --" goes to the user default direcotry (/home/username) – sdaffa23fdsf Apr 08 '12 at 22:49
  • 1
    Best answer imho : http://unix.stackexchange.com/a/180640/158177 provides cd -1 to cd -9 which I think is what the OP asked for – Titou Sep 29 '16 at 15:11
  • 1
    @sdaffa23fdsf `cd --` is equivalent to `cd`. No need to type the `--` unless you want to separate a list of paths from a list of options to avoid ambiguity. In your case, there's no such list, so the `--` is redundant. `cd` on its own changes to the home directory. – Drew Noakes Apr 25 '22 at 01:44

4 Answers4

509

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.

Snark
  • 32,299
  • 7
  • 87
  • 97
  • 31
    also pushd and popd might be useful – lorenzog Feb 25 '10 at 08:55
  • 8
    @lorenzog : lydonchandra, in his question, said "without using pushd/popd" – Snark Feb 25 '10 at 09:19
  • @ogc-nick for using this `cd --` in menu-like manner, you should use the mentioned script – RamValli Feb 11 '16 at 06:13
  • 1
    @ogc-nick no it doesn't. The `--` simply separates a command and its options from the parameters (see [this post](https://unix.stackexchange.com/a/11382/41752)). Because no arguments follow after `--`, the final command is just `cd` which switches to your home directory. That *might* have been the second previous directory, but that's just a coincidence. – Gerrit-K Mar 08 '18 at 09:02
  • wow syntactic sugar much – oldboy Jun 24 '18 at 02:00
  • you can type `d` to get list of latest directories. and `cd -n` to visit one where `n` is the index shown in `d`'s output – dipu Jul 21 '18 at 10:16
34

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

Ales Dolecek
  • 440
  • 4
  • 5
  • 7
    $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 09 '15 at 09:12
  • 1
    It is worth pointing out that using `cd $OLDPWD` does _not_ print anything to standard output, while `cd -` seems to usually do. This is a better solution for most scripts. – Bernardo Sulzbach Mar 06 '20 at 21:10
  • I dont think so!!! The `cd -` comes handy when you work in **interactive** shell so you do not have to write long commands and have feedback where you are. But for scripts you should definitelly use `$OLDPWD` as scripts usually do **not** want `cd` to print anything to stdout! Also the value of `$OLDPWD` does not need to be used just for __going back__. You can for example use it with `ls` or compare to `$HOME` or other directory. The [POSIX](https://pubs.opengroup.org/onlinepubs/009695399/utilities/cd.html) says that `cd -` shall be equivalent of `cd "$OLDPWD" && pwd`. – Ales Dolecek Mar 09 '20 at 13:31
2

I find the easiest way to do it is with this .bashrc power edit: https://github.com/wting/autojump . You get to "mark" folders you navigate to, giving them a shorthand name that's easy to remember (my advice; the foregoing is not in the docs), such as Pics for Pictures, etc. 'jump' returns you to the folder you 'marked,' and 'marks' lists folders you have added to the 'stack' (as with pushd and popd), with the added advantage that your marks remain the same from one session to the next, ad infinitum.

I have yet to try it on more than one harddrive, but the results should be similar to those using a single volume.

S Wright

0

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.

Xente
  • 127
  • 1
  • 31
    `..` is not the previous directory, it's just the parent directory. – Oliver Salzburg Dec 10 '12 at 10:28
  • 6
    This answer provides useful information even if it does not answer correctly the question. Therefore there is no point in piling negative votes on it, I upvoted for the effort. – Titou Sep 29 '16 at 12:36