Bash – How to Undo the Last cd Command

bashcommand line

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?

Best Answer

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