CDPATH

03 May 2013 in Tech

CDPATH is an environment variable that changes how the cd command works. Normally when you try and cd
into a directory, it will only try and change to the path specified from the current directory. If you spend a
lot of time working in specific folders, it might be useful to assume that your search path for cd starts from one of those folders.

For example, I spend a lot of time working in /var/www. Instead of typing cd /var/www/example.com all the time, I want to be able to
just type cd example.com and have it know what I mean. Here's how to set that up:

bash
export CDPATH=.:~:/var/www

Now, you can just type cd example.com and it will look for ./example.com then /var/www/example.com, stopping whenever it finds a match
or runs out of search locations.

You can have as many base search paths as you like. Here's one that includes /etc as a search path too.

bash
export CDPATH=.:~:/var/www:/etc
vagrant@precise64:~$ pwd
/home/vagrant
vagrant@precise64:~$ cd example.com
/var/www/example.com
vagrant@precise64:/var/www/example.com$ cd mysql
/etc/mysql
vagrant@precise64:/etc/mysql$ cd Downloads
/home/vagrant/Downloads