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.

18 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


  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?

  15. MikeC says:

    That’s not the same and wouldn’t make sense for the author to mention the “I’m in dir /x/y/z/a and made a cd by mistake)

    cd .. after this would just go to “/home” assuming you’re now in “/home/user” (aka ~)

    cd – goes back IN HISTORY if you want a better explanation….

    So when you type “cd /a/b/c” and then “cd /x/y/z”: “cd -” will go back to “/a/b/c” while cd .. would just got to “/x/y/”

    I go to admit I don’t remember to use it as often as I should, mostly because I use freebsd more and “csh/tsh” go back in the history of a command when you do “command – Arrow UP” which is many times helpfully but not always.

    Anyway this is old but the comment might confuse other users so I though I should explain that is not the same thing at all!

  16. MikeC says:

    “I go to admit” should read “I have to admit”*

    Also that reply was to @balalayka ofc

    @Carlos AFAIK no, that’s not possible with DOS “cd”!

Leave a Reply