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 ((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)) 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!)

24 thoughts on “Unix/Linux trick: ‘cd’ back to the previous directory”

  1. 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. You also have pushd and popd to push and pop the directory you’re in on the stack, it gives you even more flexibility

  3. 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 -‘.

  4. 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!

  5. 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.

  6. 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.

  7. 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…

  8. 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!

  9. “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”!

  10. “cd ~-” will perform the cd without echoing the direcotry to the screen. Useful when using in scripts.

  11. Also:
    in zsh you can
    cd -5
    and it will cd to the fifth last directory. And
    cd –
    will show a list of the last directories and I can pick one. Bash does not seem to support “cd -” (maybe it has a different syntax).

  12. In Ubuntu 18.04, I added these aliases to my ~/.bashrc file:

    alias u=’cd ../’
    alias t=’cd /’
    alias h=’cd ~’
    alias b=’cd -‘

    Works a treat πŸ™‚

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.