Wednesday, January 19, 2011

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 
/home/ramesh 
 
[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 
/etc/mail 
 
[Note: This is looking for mail under /etc and not 
under current directory] 
 
[ramesh@dev-db /etc/mail]# pwd 
/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. 
 
export CDPATH=.:~:/etc:/var 
 
This hack can be very helpful under the following situations: 
o    Oracle DBAs frequently working under $ORACLE_HOME, can set 
the CDPATH variable to the oracle home 
o    Unix sysadmins frequently working under /etc, can set the 
CDPATH variable to /etc 
o    Developers frequently working under project directory 
/home/projects, can set the CDPATH variable to /home/projects 
o    End-users frequently accessing the subdirectories under their 
home directory, can set the CDPATH variable to ~ (home 
directory)  

0 komentar:

Post a Comment