Thursday, November 19, 2009

$CDPATH - very useful for bash shell navigation

Often, we “cd” to commonly used directories in a bash shell. Having to type the whole path is often a pain. I was using aliases to navigate to commonly used directories (e.g. alias scripts=’cd /cygdrive/c/scripts’). But this would require you to identify each frequently used directory, define an alias for each one of them and remember the alias.

 

A good friend of mine, pointed out an alternate to achieve the same. $CDPATH. This is a set of paths that the “cd” command would look into (besides “.”) for the directory to switch to. So if “cd scripts” is executed, it will check if the current directory has a “scripts” directory, if not, it will look for a “scripts” directory in each directory indicated in the “CDPATH” variable. If “CDPATH” has “/cygdrive/c/: /cygdrive/c/home/xyz”, then cd will look for “./scripts”, “/cygdrive/c/scripts”, “/cygdrive/c/home/xyz/scripts” and cd to the first directory that exists.

 

Source.

 

No comments: