Unix/Linux trick: ‘cd’ back to the previous directory

You know when you’re in a very “deep” directory, such as
/usr/local/src/this/that/thatother, and you type “cd” and press enter by mistake(1) and go back to your home directory, and would love to go back to where you were before, without having to type all the path again (or copy and paste it, or do a CTRL-R back search)?

Just type “cd -” (that’s a single dash, or “minus” sign). Believe it or not, I didn’t know that until today, and I’ve used Linux since 1994 or so. Slightly embarrassing, I know. :)

If you enter the command a second time, you will return to where you were before typing the first “cd -“. In other words, the command can be used to toggle between the previous directory and the current one.

It’s also not just a bash thing; I’ve tried it on FreeBSD’s sh and OpenBSD’s default ksh, and it works there as well.

(Found here, after someone asked me and I didn’t know the answer.)

P.S: – Welcome to the first technical post on Winterdrake. (Hey, it’s geeky stuff, too!)

  1. happens a lot when you think there’s only one directory where you are, so you type "cd <tab><enter>“, but there was in fact more than one, and the tab key didn’t add anything []

This entry was posted in Computers, Linux, Unix and tagged , . Bookmark the permalink.

16 Responses to Unix/Linux trick: ‘cd’ back to the previous directory

  1. Alex says:

    I do this all the time, and likewise have used linux for a number of years but I never thought it this was possible because I had never asked my self. I would just do it the long way and go back into my deep directory. Thank you.

  2. Mahesh says:

    cool! Thanks a lot.

  3. PBJ says:

    Works from git shell on Windows 7

  4. Frode says:

    You also have pushd and popd to push and pop the directory you’re in on the stack, it gives you even more flexibility

  5. Martin Thoma says:

    Very nice! Thank you very much!

  6. maximmaI says:

    Very helpful, thank you!

    I didn’t know until today.

  7. karatedog says:

    It is not just the previous dir and the other.

    Use ‘pushd’ with a directory argument. It will push the current dir and the argument to a ‘stack':
    /home/user/work > pushd /home/user/work/this/that/whatever
    ~/work ~/work/this/that/whatever

    Now you can go back and forth between those 2 using ‘cd -‘.

  8. I liked this trick, very useful, thanks a lot. :)

  9. teh (@teh) says:

    Also usually available in OLDPWD in the environment, according to SUSv4, cd – is equvalent to

    cd “$OLDPWD” && pwd

    http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cd.html

  10. Evaryont says:

    My favorite trick to complement this is an alias, specifically ‘-‘ as an alias. Bash and Zsh, not sure about other shells:

    alias — -=’cd -‘

    Now all you have to do is type ‘-‘ and you’re back at the previous directory. Very quick and handy!

  11. I’ve always used $OLDPWD but rarely bother with it as I’ll just open another putty window if I really don’t want to leave the current directory. But yeah, I didn’t know this one either.
    FreeBSD’s man page for bash:
    An argument of – is converted to $OLDPWD before the directory change is attempted.
    Kind of like when I realized hitting “0” took me to the beginning of the line in vi. I never used “^” again.

  12. You can also use ~- as a variable referring to $OLDPWD, so you can do somehting like “cp ~-/foo .” to copy the file named foo from the previous current directory into the current directory.

  13. balalayka says:

    I feel iam gonna blow up uyour thread with saying ‘cd ..’ is back one dir. And I use linux’s crap on stupid android a day or so…

  14. Carlos says:

    Is there any equivalent parameter for “CD” in MS-DOS?

Leave a Reply