Skip to Content

Navigate Efficiently and Save Time with CDPATH

July 7, 2024

I've saved myself some keystrokes using CDPATH. It’s like your PATH variable, but instead of trying to locate a program, your shell is checking CDPATH as it searches for the directory that you are trying to change into. By using this, you'll be able to navigate into deeply nested directories without specifying their full path. Normally, if you run "cd", and pass just a directory name as an argument, then the directory won't be found unless it's present in the current working directory. However, If the path of its parent directory is a part of your CDPATH, then it will be found.

Say you have a “sites” directory that consists of several projects that you frequently access. For the sake of example, it’s deeply nested, and you normally must type a long path to get to it. Using CDPATH, you could just run “cd project1” and your working directory would be something like /Users/user/a/bit/nested/sites/project1. Something neat is that from within project1, you could run “cd project2”, even though it’s not located within project1, and you'd change directories into project2.

By adding the path of the parent directory of these projects to our CDPATH, we are able to navigate more efficiently. We could use aliases, but we would be adding an alias for every shortcut rather than just one addition to CDPATH. Or, we could add an alias for sites, but then we'd run an additional command to change directories into a project.

You can use this if you're running macOS, a GNU/Linux OS, or even Windows with Windows Subsystem for Linux (WSL).

Here's how you can set this up:

I'm on a Mac, and I'm using Zsh. I know that I have a .zshrc file in my home directory, but you may need to check if you have one or not. Assuming you are in your home directory, you can check for and create the file, if need be, using the following command:

test -e .zshrc || touch .zshrc

The next thing to do is edit the .zshrc file and define the CDPATH variable. The syntax is just like the PATH syntax.


Note: Variables are typically named in uppercase. There's no space around the "=" operator. Paths are separated by colons.

Unless you exit your terminal and open a new one, you're going to need to source that .zshrc file. You can do that by running the command below.

source .zshrc

That's it for Zsh. Now, if you're using Bash on your local machine, then the process is the same, except that the filename is .bashrc.

If you want to try this on a server running Linux, then your user's shell is likely set to Bash. Follow the same process, but also make sure that .profile in your home directory is sourcing .bashrc, so that when you log in, your CDPATH variable will be defined.

That's the gist of it. Using CDPATH allows us to navigate a filesystem more efficiently by typing less. If I've left anything out, or you're having trouble getting it to work, then please just comment below. I'd be happy to help. Thanks!

Post a comment