33

Possible Duplicate:
Go back to previous directory in shell
How can I change to the previous directory instead of going up?

is there a command line way to navigate back to the directory I was in?

I frequently do cd .. to go up a directory, but sometimes I accidentally just do cd which takes me to the user's home directory.

I want to go back to the directory I was just in, without navigating all the way through the nested folders or without typing the whole path out, which can be very long and prone to errors

I am specifically using OSX, but cd is pretty universal

3
  • Try tipying a cd /? on your OSX. I did it on Windows but cd don't have such feature.
    – Diogo
    Jun 11, 2012 at 15:40
  • @Diogo That command does not work on OS X (or any Unix/Linux).
    – slhck
    Jun 11, 2012 at 16:17
  • @slhck I never used OSX so I really dont know.. My intent was to tell to type a cd -help or something like this to show cd manual.
    – Diogo
    Jun 11, 2012 at 16:26

2 Answers 2

65

On *nix systems, you can type cd - to return to the previous directory. Doesn't work on Windows, though.

2
  • 2
    it does on git bash for windows (at least now in 2019). thanks for the tip
    – keinabel
    Jan 28, 2019 at 11:57
  • @HosseinEbrahimi 'cd ..\' goes up to the parent directory, not back to the previous directory.
    – Indrek
    Oct 30, 2022 at 11:41
15

Try:

cd -

That works in Bash. It may work in your shell too.

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