|Home||Switchboard||Unix Administration||Red Hat||TCP/IP Networks||Neoliberalism||Toxic Managers|
May the source be with you, but remember the KISS principle ;-)
Skepticism and critical thinking is not panacea, but can help to understand the world better
|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.
The variable CDPATH defines the search path for the directory containing directories. So it served much like "directories home". The dangers are in creating too complex CDPATH. Often a single directory works best. For example export CDPATH = /srv/www/public_html . Now, instead of typing cd /srv/www/public_html/CSS I can simply type: cd CSS
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 directory,
is a shortcut to this location for the given example.
Dr. Nikolai Bezroukov
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]$
Google matched content
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
The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D
Copyright Š 1996-2018 by Dr. Nikolai Bezroukov. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) in the author free time and without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.
This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...
|You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info|
The statements, views and opinions presented on this web page are those of the author (or referenced source) 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: February 05, 2017