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

by Ramesh

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.

  1. [ramesh@dev-db ~]# pwd
  2. /home/ramesh
  3.  
  4. [ramesh@dev-db ~]# cd mail
  5. -bash: cd: mail: No such file or directory
  6.  
  7. [Note: This is looking for mail directory under current directory]
  8.  
  9. [ramesh@dev-db ~]# export CDPATH=/etc
  10. [ramesh@dev-db ~]# cd mail
  11. /etc/mail
  12.  
  13. [Note: This is looking for mail under /etc and not under current directory]
  14.  
  15. [ramesh@dev-db /etc/mail]# pwd
  16. /etc/mail

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.

  1. $ 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)