Change Back to Previous Directory

Navigate to the directory you visited previously without the need of remembering or typing the name.

There’s a super fast and easy-to-remember way to return to the previous directory.

cd -

This is useful when you need to go into another directory on your machine and then come back to where you were working, without needing to type in the full path.

Changing into Previous Directory Example

For example, say I change into some working project directory.

cd ~/workspace/my-new-project

But then I want to switch to the machine’s tmp directory to run a quick test.

cd /tmp

Now if I want to go back to the project directory, all I have to do is this:

cd -
# ~/workspace/my-new-project

And since - represents the previous directory, I could run it again to get back to the tmp directory:

cd -
# /tmp

Tip: Make Using Full Paths a Habit

Note that this trick is less beneficial if you tend to change directories in multiple commands. For example, rather than run cd ~/workspace/my-new-project, I’ve seen developers do this:

cd ~
cd workspace
cd my-new-project

Although inefficient, it works and will get you where you need to go. But it makes this trick less desirable. Following those commands, if you were to now run cd -, you would end up in ~/workspace, rather than wherever you were prior to running cd ~.

Let's Connect

Keep Reading

Transforming Unsplash Images

Unsplash uses Imgix to serve its images, which means you can transform the images before downloading.

Dec 08, 2022

Pro Tip: Use “1.” for Every Ordered List Item when Writing Markdown

A quick tip for making ordered lists in markdown easier to write.

May 31, 2022

Use Commit Hashes when Sharing GitHub URLs

Sharing GitHub commit hashes directly works for ephemeral uses, but there is a safer way for links that need to last.

Dec 15, 2022