Command line, go back to last directory

20

6

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

cqm

Posted 2012-06-11T15:32:36.730

Reputation: 1 107

Question was closed 2012-06-11T16:10:46.057

Try tipying a cd /? on your OSX. I did it on Windows but cd don't have such feature. – Diogo – 2012-06-11T15:40:53.440

@Diogo That command does not work on OS X (or any Unix/Linux). – slhck – 2012-06-11T16:17:33.067

@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 – 2012-06-11T16:26:59.030

Answers

43

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

Indrek

Posted 2012-06-11T15:32:36.730

Reputation: 21 756

1it does on git bash for windows (at least now in 2019). thanks for the tip – keinabel – 2019-01-28T11:57:02.440

11

Try:

cd -

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

Fran

Posted 2012-06-11T15:32:36.730

Reputation: 4 774