Tuesday, April 17, 2012

What is CDPATH ?



    Has it happened to you wherein you logged in to an account or to an environment and simply printed the command, say "cd log" and your directory gets changed to some log directory. The interesting part is: The log directory is not there in the directory from where you fired the cd command. It was somewhere also, however the cd command switched to it correctly. The question now is:  If the "log" directory is not present in the location from where we are giving the "cd" command, how did we reach the log directory?  The answer to this question is most probably  the CDPATH environment variable would have been set.

1. What is CDPATH?
       CDPATH is an environment variable. It looks exactly like the PATH variable containing with many different paths concatenated using ':'.

2. What is the use of CDPATH ?
       Say, for example, some directories present in an "X" directory are being accessed frequently by the user. Every time the user wants to traverse to any of these directories present in "X",  he mostly traverses by giving the absolute path which is little time consuming if you have to find it. How nice will it be if we can do "cd" to the particular directory as if that directory which you are looking for is right under your current directory. This is what the CDPATH achieves.

    Normally, when the cd command is given, it checks for the directory name in the current directory and throws error if not found, else traverses to the directory. If the CDPATH is set, the "cd" command starts finding the directory in the list of directories present in the CDPATH variable and then makes the directory switch appropriately.

3. How to set the CDPATH variable?
     Setting the CDPATH variable is exactly like setting the PATH variable. For example:
$ export CDPATH=".:/home/guru:/usr"
     This above command will hold good till the session is closed. In order to make this change permanent, we need to put this command in the appropriate profile file. The CDPATH command in this example is set with 3 components: The current directory followed by /home/guru and the /usr.

  Let us try to test this CDPATH variable. Currently, we are in /home/guru. There are 3 directories present under it.
$ pwd
/home/guru
$ ls -l | grep ^d
drwxr-xr-x+ 1 guru None       0 Feb  9 20:18 C
drwxr-xr-x+ 1 guru None       0 Apr 15 13:56 bin
drwxr-xr-x+ 1 guru None       0 Apr 15 18:35 perl
Now, let us try to switch to the perl directory. And hence we will be in /home/guru/perl. The switch to "perl" directory happened because the CDPATH contains the dot(.) at first place, and perl is under current directory(.)
$ cd perl
$ pwd
/home/guru/perl
Now, let us try to switch to the bin directory which is under home whereas we are actually now under perl.
$ cd bin
$ pwd
/home/guru/bin
    This is how the CDPATH works. This is highly useful when you have some frequently traversed directories in the environment.

4. How to find the value of the CDPATH variable?
$ echo $CDPATH
.:/home/guru:/usr
5. If CDPATH is set, will the cd command still search in the current directory by default before referring to the CDPATH?
      No. Once the CDPATH is set, the cd command will search only in the directories present in the CDPATH variable only. Hence, it is the responsibility of the user to make sure that the current directory is also present in the CDPATH variable.
Note: This point holds good when only the directory name is specified with cd. cd when given with absolute or relative path does not refer to the CDPATH command.

6. Can the current directory be anywhere in the CDPATH variable?
      As such, there is no rule to put the current directory in specific place. However, it SHOULD always be the first component of the CDPATH. It is because if it is not there in first position, the default functioning of the cd command which is to switch to the required directory under current directory may not work. Hence, the cd command should always be made to search for the directory first in the current directory, and then only in the other directories present in the CDPATH variable.
Related Posts Plugin for WordPress, Blogger...

No comments:

Post a Comment