How to set and use the Linux CDPATH variable with bash

The CDPATH is an environment variable, just like PATH. It is used to cd directly in the folders you add in your CDPATH.

Watch Free Movies

By default, your CDPATH is empty but you can easily set it, as you like.

Difference between PATH and CDPATH:

You can exec, read and write your files and folders set in the PATH directories, but you can just cd (exec) in the directories inside the directories set in the CDPATH.

In other words, the PATH is usable with every command, while the CDPATH works only for cd.

How to display the current CDPATH:

$ echo $CDPATH

How to set the CDPATH:

I have this directory structure and will add ~/my in the CDPATH:

$ tree ~/my | head -4
/home/razvan/my
├── ebooks
├── school
└── work

$ export CDPATH=$CDPATH:/path/to/dir

$ export CDPATH=$CDPATH:/home/razvan/my
$ echo $CDPATH
/home/razvan/my

Demonstration of Power:

$ pwd
/root
$ cd work
/home/razvan/my/work
$ cd school/
/home/razvan/my/school
$ cd ebooks/
/home/razvan/my/ebooks

To add more than one directory to the cdpath, separate the to be added directory paths with colon (:) :

export CDPATH=$CDPATH:/path/to/dir1:/path/to/dir2:/path/to/dir3

To clear the CDPATH, use export CDPATH=”” .

How to set the CDPATH persistenly:

If you need to set your CDPATH persistently, add “export CDPATH=$CDPATH:/path/to/new/dir” in your ~/.bashrc file.

Scroll to Top