# CDPATH: Easily Navigate Directories in the Terminal

*Published on December 05, 2023*

---


🔥 **TL;DR:**&nbsp;Set CDPATH with your most important or frequently used parent directories, and you can directly cd into them or their subdirectories no matter where you are in the file system. You don’t have to type the full path anymore.

You know about the&nbsp;`PATH`&nbsp;environment variable. Did you know about&nbsp;`CDPATH`? If you’ve been typing long&nbsp;`cd`&nbsp;commands like me, this post is for you.

I came across this gem while reading Daniel Barrett’s&nbsp;[Efficient Linux at the Command Line](https://learning.oreilly.com/library/view/efficient-linux-at/9781098113391/). It instructs the&nbsp;`cd`&nbsp;command to search for the directory you specify in locations other than your current directory.

A&nbsp;`cd`&nbsp;search path works like your command search path&nbsp;`$PATH`. However, instead of finding commands, it finds the subdirectories. You can configure it with the shell variable&nbsp;`CDPATH`, and it has the same format as the&nbsp;`PATH`&nbsp;variable: a list of directories separated by colons.

Set this variable in your&nbsp;`~/.zshconfig`&nbsp;or&nbsp;`~/.bashrc`&nbsp;file to include the directories you visit most frequently.

```bash
export CDPATH=$HOME:$HOME/software:$HOME/software/ruby:$HOME/software/rails:$HOME/software/youtube
```

Now, whenever you want to&nbsp;`cd`&nbsp;into a directory, the shell will look through all the above places in addition to the current directory.

What’s more, the search is lightning fast. It looks only in parent directories that you specify and nothing else.

For example, let’s say you have a&nbsp;`$HOME/software/blog`&nbsp;directory and you’ve configured the&nbsp;`CDPATH`&nbsp;to include the&nbsp;`$HOME/software`&nbsp;directory.

Now, if you type&nbsp;`cd blog`&nbsp;from anywhere in the filesystem, the&nbsp;`cd`&nbsp;command will take you to the&nbsp;`$HOME/software/blog`&nbsp;directory, unless it finds another&nbsp;`blog`&nbsp;directory in another pre-configured path.

> 💡The order of CDPATH matters. If two directories in $CDPATH have a subdirectory named blog, the earlier parent wins.

In the above example,&nbsp;`cd`&nbsp;will check the existence of the following directories in order, until it finds one or it fails.

1. in the current directory
2. `$HOME/software/blog`
3. `$HOME/software/ruby/blog`
4. `$HOME/software/rails/blog`
5. `$HOME/software/youtube/blog`

Trust me, this is pretty freaking cool. It has changed the way I use the shell.

P.S. Don't overdo it. If you put too many directories with common names into CDPATH and&nbsp;`export`&nbsp;it,&nbsp;[it might break your scripts](https://news.ycombinator.com/item?id=19886334).

