May the source be with you,
but remember the KISS principle ;-)
Key Softpanorama Topics
|About||Contents||Top Updates||Top Visited|
|Bulletin||Selected Papers||Softpanorama Bookshelf||History|
|News||Advanced Unix filesystem navigation||Recommended Links||ln||Command completion||History Substitution||pushd/popd/dirs|
Norton Change Directory
|OFM||Dotfiles||Unix shells history||Unix Shell Tips and Tricks||Humor||Etc|
The shell variable CDPATH is a useful but extremely underutilized and often misunderstood feature of ksh and bash.
This variable is similar to PATH and provides is a list of paths that the "cd" command will search for whatever subdirectory you provide as its argument. For example, if you can have:
you can "cd" to any of the subdirectories in the
/etc by typing
cd [subdirectory] without typing
the entire path - regardless of your current directory. Among the most
natural candidates for the includtion into $CDPATH are:
./ -- The current directory (see the warning below)
~/ -- Your home directory, if you use subdirectories in it (you should ;-)
../ -- The parent directory
/fav/ -- A root level directory containing nothing but symbolic links to other commonly used directories (favorites) and files.
So the initial CDPATH might look like :
What happens if we have a local directory with the same name as one of the others defined in $CDPATH? What I have found that in ksh and bash:
that means that it's a very good idea to use those trailing slashes if you want to change to local subdirectories.
|It's a very good idea to use those trailing slashes if you want to change to local subdirectories.|
Also note that CDPATH concept works
for partial pathnames too. If you have a /usr/local/lib/X11
cd lib/X11 is a shortcut to this location for the
Dr. Nikolai Bezroukov
|Bulletin||Latest||Past week||Past month||
quote from O'Reilly book Safari Books Online - 0596526784 - bash Cookbook, 1st Edition
You want to make it easier to switch between several directories in various locations.
Set your $CDPATH appropriately. Your commonly used directories will likely be unique, so for a contrived example, suppose you spend a lot of time working with init's rc directories:/home/jp$ cd rc3.d bash: cd: rc3.d: No such file or directory /home/jp$ export CDPATH='.:/etc' /home/jp$ cd rc3.d /etc/rc3.d /etc/rc3.d$ cd rc5.d /etc/rc5.d /etc/rc5.d$ /etc/rc5.d$ cd games bash: cd: games: No such file or directory /etc/rc5.d$ export CDPATH='.:/etc:/usr' /etc/rc5.d$ cd games /usr/games /usr/games$
According to the bash Reference, $CDPATH is "a colon-separated list of directories used as a search path for the cd built-in command." Think of it as $PATH for cd. It's a little subtle, but can be very handy.
If the argument to cd begins with a slash, $CDPATH will not be used. If $CDPATH is used, the absolute pathname to the new directory is printed to STDOUT, as in the example above.
Warning: Watch out when running bash in POSIX mode (e.g., as /bin/sh or with --posix). As the bash Reference notes:"If $CDPATH is set, the cd built-in will not implicitly append the current directory to it. This means that cd will fail if no valid directory name can be constructed from any of the entries in $CDPATH, even if a directory with the same name as the name given as an argument to cd exists in the current directory."To avoid this, explicitly include . in $CDPATH. However, if you do that, then another subtle point noted in the bash Reference comes into play:"If a nonempty directory name from $CDPATH is used, or if '-' is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output."In other words, pretty much every time you use cd it will echo the new path to STDOUT, which is not the standard behavior.
Common directories to include in $CDPATH are:
The current directory (see the warning above)
Your home directory
The parent directory
The grandparent directory
A hidden directory containing nothing but symbolic links to other commonly used directories
The above suggestions result in this:export CDPATH='.:~/:..:../..:~/.dirlinks'
16.5.4. See Also
Some people make a shell alias (Section 29.2) for directories they cd to often. Other people set shell variables (Section 35.9) to hold the pathnames of directories they don't want to retype. But both of those methods make you remember directory abbreviations and make you put new aliases or shell variables in your shell startup files (Section 3.3) each time you want to add or change one. There's another way: the C shell's cdpath shell variable and the CDPATH variable in ksh, bash, and some versions of sh. (zsh understands both cdpath and CDPATH.) I'll use the term "cdpath" to talk about all shells.
When you type the command cd foo, the shell first tries to go to the exact pathname foo. If that doesn't work, and if foo is a relative pathname, the shell tries the same command from every directory listed in the cdpath. (If you use ksh or sh, see the note at the end of this article.)
Let's say that your home directory is /home/lisa and your current directory is somewhere else. Let's also say that your cdpath has the directories /home/lisa, /home/lisa/projects, and /books/troff. If your cd foo command doesn't work in your current directory, your shell will try cd /home/lisa/foo, cd /home/lisa/projects/foo, and cd /books/troff/foo, in that order. If the shell finds one, it shows the pathname:% cd foo /home/lisa/foo %
If there is more than one matching directory, the shell uses the first match; if this isn't what you wanted, you can change the order of the directories in the cdpath.
Some Bourne shells don't show the directory name. All shells print an error, though, if they can't find any foo directory.
So, set your cdpath to a list of the parent directories that contain directories you might want to cd to. Don't list the exact directories list the parent directories (Section 1.16). This list goes in your .tcshrc, .cshrc, or .profile file. For example, lisa's .tcshrc could have:
~ Section 31.11set cdpath=(~ ~/projects /books/troff)
A Bourne shell user would have this in his .profile file:CDPATH=:$HOME:$HOME/projects:/books/troff export CDPATH
A bash user might have it in her .bashrc or .bash_profile.
(If your system doesn't define $HOME, try $LOGDIR.)
Note that the Bourne shell CDPATH in the above example starts with a colon (:) which, as in the PATH variable, is actually an empty entry (Section 35.6) that stands for "the current directory." Both the sh and ksh I tested required that. Without an empty entry, neither sh or ksh would cd into the current directory! (bash seemed to work like csh, though.) You could actually call this a feature. If there's no empty entry in CDPATH, a user has to use cd ./subdirname to go to a subdirectory of the current directory.
JP and SJC
Another interesting trick is the
$CDPATHvariable. Similar in nature to the
$PATHvariable, it allows an argument to the '
cd' command to be considered not only in the current working directory, but in an ordered list of directories!
It is not straightforward to see the utility of this feature, but bear with me. Each user has a particular area of interest on a given Linux system. A system administrator (root) would be interested in
/var/log. A home desktop user (joe) would be interested in
~/Documents. Someone at work (juser) might spend most of his or her time in a
So, by placing each user's commonly visited paths in that user's
$CDPATHvariable, one can change to a known subdirectory without first having to navigate there! For example:
(pwd = /home/juser)
(pwd = /usr/local/projects/abc)
(pwd = /usr/local/projects/abc/src/db/old/migration/)
(pwd = /usr/local/projects/abc)
Indeed, one can find many subtle uses for this feature. A reasonable list to start with on a Red Hat Linux system, for example, might be
*NOTE: An empty entry ('::' or a leading or trailing ':') in $CDPATH (or in $PATH) is interpreted as the current working directory!
Something you may have seen before in other systems (the much maligned SCO OSes, for example) is this handy option:
shopt -s cdspell
"This will correct minor spelling errors in a '
cd' command, so that instances of transposed characters, missing characters and extra characters are corrected without the need for retyping."[jason@localhost jason]$ cd Documnts Documents [jason@localhost Documents]$[jason@localhost jason]$ cd documents Documents [jason@localhost Documents]$
Unfortunately, it's flexibility is rather limited...[jason@localhost jason]$ cd document bash: cd: document: No such file or directory [jason@localhost jason]$
Please visit Heiner Steven SHELLdorado the best shell scripting site on the Internet
Advanced Bash-Scripting Guide
Advanced Unix filesystem navigation
bash Tips and Tricks
Pushd, popd and dirs
Bash and ksh93 Shell Prompts Customarization
Unix Shell Tips and Tricks
Most popular humor pages:
Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : C++ Humor : ARE YOU A BBS ADDICT? : Object oriented programmers of all nations : C Humor : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor: Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : The Most Comprehensive Collection of Editor-related Humor : Microsoft plans to buy Catholic Church : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor : Best Russian Programmer Humor : Russian Musical Humor : The Perl Purity Test : Politically Incorrect Humor : GPL-related Humor : OFM Humor : IDS Humor : Real Programmers Humor : Scripting Humor : Web Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor :
The Last but not Least
|You can use PayPal to make a contribution, supporting hosting of this site with different providers to distribute and speed up access. Currently there are two functional mirrors: softpanorama.info (the fastest) and softpanorama.net.|
The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: July 07, 2013