Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there any way to jump back to the previous working directory after doing a cd to a different directory?

My use-case is that I'm traversing a directory structure for a Java project, and I'm expecting that the current directory has only one sub-directory. So, I type cd and hit Tab and Enter rather quickly. However, the Tab failed, as I mistakenly expected one sub-directory, when in fact there are more. So now, I just executed cd, and am now in my home directory, instead of the Java project. Is there a way to "undo" this cd and jump back to the directory I was in?

share|improve this question

4 Answers 4

up vote 65 down vote accepted

You can use cd - to go back to your previous location, no matter where that was.

share|improve this answer
1  
Awesome - That's exactly what I was looking for. Thanks. –  nickb Jul 16 '12 at 18:24
5  
Well ...even after years on Ubuntu I did not know that. Thanks –  andybleaden Jul 24 '12 at 9:40

cd - is great for going back one level, but if you find yourself wanting to go back a few levels, check out this script:

http://www.geocities.com/h2428/petar/bash_acd.htm

It's great. cd -- to see your history, and cd -3 to go back 3 levels, for example.

share|improve this answer

You can also use pushd and popd to utilize the directory stack :)

Some info here

share|improve this answer

In addition, cd .. can take you back to the working directory's parent directory and then if necessary the command can be repeated until you get to where you want to be. In fact, each time it is evoked it takes you back through the directory tree, which would ultimately finish at / (the root directory) if you just kept repeating the command.

In contrast, cd - actually makes the previous working directory the current working directory, (which is known as .), and which exact location in the filesystem can be found with pwd.

So both cd .. and cd - can be useful in different circumstances when navigating the directory tree, although they do differ in the aforementioned respects. Perhaps the most useful command after having fun experimenting with these two commands is cd, which returns you to the home folder.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.