Hack 1. Use CDPATH to define the base directory for cd command

If you are frequently performing cd to subdirectories of a specific parent directory, you can set the CDPATH to the parent directory and perform cd to the subdirectories without giving the parent directory path as explained below.

[ramesh@dev-db ~]# pwd

[ramesh@dev-db ~]# cd mail
-bash: cd: mail: No such file or directory

[Note: This is looking for mail directory under current directory]

[ramesh@dev-db ~]# export CDPATH=/etc
[ramesh@dev-db ~]# cd mail

[Note: This is looking for mail under /etc and not under current directory]

[ramesh@dev-db /etc/mail]# pwd

To make this change permanent, add export CDPATH=/etc to your ~/.bash_profile

Similar to the PATH variable, you can add more than one directory entry in the CDPATH variable, separating them with : , as shown below.

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

This hack can be very helpful under the following situations:

  • Oracle DBAs frequently working under $ORACLE_HOME, can set the CDPATH variable to the oracle home
  • Unix sysadmins frequently working under /etc, can set the CDPATH variable to /etc
  • Developers frequently working under project directory /home/projects, can set the CDPATH variable to /home/projects
  • End-users frequently accessing the subdirectories under their home directory, can set the CDPATH variable to ~ (home directory)

Comments on this entry are closed.

  • Francois Begin April 5, 2010, 1:00 pm

    The order in which you define the directories with CDPATH determines where you end up. For instance, assume you have set CDPATH to this


    If you are located at the root of the filesystem (/) and type in ‘cd tmp’, you will end up in /tmp since the ‘cd’ command matches ‘./tmp’ first.

    If you were in /etc when you type ‘cd tmp’, then ‘cd’ would look for ‘./tmp’ i.e. ‘/etc/tmp’ first. If not found, it would then look for /var/tmp. If not found, it would finally look for ~/tmp.

    Just keep that in mind if you have multiple entries in CDPATH and try to go into a subdirectory with a common name such as ‘log’, ‘tmp’, etc.

  • Jonathan January 19, 2011, 6:54 pm

    Exporting CDPATH has the unfortunate side effect of breaking some utilities implemented as shell scripts. (There are two effects shell script authors might not be prepared for: “cd” suddenly jumps to unexpected directories, and it writes text to the standard output.)

    By contrast, a simple

    if test “${PS1+set}”; then CDPATH=whatever; fi

    in .bashrc sets CDPATH in interactive shells, without exporting it to noninteractive scripts.

    From here