CDPATH

This article was published 6 years ago. Due to the rapidly evolving world of technology, some concepts may no longer be applicable.

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:

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.

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

[email protected]:~$ pwd
/home/vagrant

[email protected]:~$ cd example.com
/var/www/example.com

[email protected]:/var/www/example.com$ cd mysql
/etc/mysql

[email protected]:/etc/mysql$ cd Downloads
/home/vagrant/Downloads

Thoughts on this post

Leave a comment?