Mini-article series #1: Smarter directory switching

Dipam Vasani
4 min readNov 15, 2022
Photo by Christopher Gower on Unsplash

In this article you will learn a few ways to navigate directories faster using your terminal.

Basics recap

Let’s go over the basics first.

pwd or print working directory is a command that let’s you check what directory you are currently in.

cd or change directory let’s you navigate from one directory to another

.. refers to the parent directory, when you run cd .. you go back one directory.

You can chain a bunch of .. together to go back many directories in your directory tree.

If you run just cd from any directory, it will take you to your home directory. You can also switch to your home directory using cd ~ where ~ represents your home directory.

A useful plugin to enable is autocomplete. If you run the same commands over and over again, it’s faster to autocomplete them.

Finally, note that everything is a path in your directory tree. Think about screenshots as not a standalone folder but a path from your home directory to the screenshots folder.

tree is a useful command to view the directory structure.

Smarter switching #1: Jumping between two directories

Say that while writing this medium article, I switch a lot between two directories images and smarter_switching as shown in the tree below.

To do this

  1. Start in one of these directories and navigate to the other

2. then you can simply run cd - to jump between these two directories

Smarter switching #2: Returning to a primary directory

My default shell is zsh . For this tip I will switch to bash since zsh and some plugins I have installed behave differently in this case.

For this example, I want to treat smarter_switching as my primary directory. I usually work in this directory but occasionally navigate to other directories only to return back to this one.

You would do this as follows:

  1. Make sure you are in your primary directory

2. Save the location of this directory using pushd

Use pushd similar to how you use cd . This does two things.

  • Push the current directory on to a directory stack (Last in First out)
  • Navigate to a different directory (just like cd).

You can view this stack using the dirs command.

3. Navigate to a bunch of directories

4. Use popd to pop the saved directory from the stack and return to it

Check the links at the end of this article for more tips wrt directory stack.

Smarter switching #3: Jumping anywhere

Oh my zsh has a plugin called autojump that maintains a database of directories you’ve navigated to and then let’s you jump to them from anywhere.

Once installed you can run it as j <directory name>

In this example I will jump to the screenshots folder from different locations.

  1. From my home directory

Notice that I did not even have to type the whole directory name, I just typed screen , pressed Tab and then Enter and it worked. Let’s try another

2. From the folder november

3. From downloads

That will be it for this article :)

~happy learning

References and useful links:

--

--