Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use CDPATH to access frequent directories in bash UNIX
I often find myself wanting to cd to the various directories beneath my home directory (i.e. ~/Library, ~/Music, etc.), but being lazy, I find it painful to have to type the ~/ if I'm not in my home directory already. Enter CDPATH, as desribed in man bash):
The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is ".:~:/usr".
Personally, I use the following command (either on the command line for use in just that session, or in .bash_profile for permanent use):
CDPATH=".:~:~/Library"
This way, no matter where I am in the directory tree, I can just cd dirname, and it will take me to the directory that is a subdirectory of any of the ones in the list. For example:
$ cd
$ cd Documents 
/Users/baumanj/Documents
$ cd Pictures
/Users/username/Pictures
$ cd Preferences
/Users/username/Library/Preferences
etc...
[robg adds: No, this isn't some deeply buried treasure of OS X, but I'd never heard of the CDPATH variable, so I'm assuming it will be of interest to some other readers as well.]
    •    
  • Currently 3.00 / 5
  You rated: 1 / 5 (7 votes cast)
 
[23,496 views]  

Use CDPATH to access frequent directories in bash | 7 comments | Create New Account
Click here to return to the 'Use CDPATH to access frequent directories in bash' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use CDPATH to access frequent directories in bash
Authored by: zimmerleut on Mar 21, '05 10:46:24AM

This is cool, but is there a way to enable bash's name completion with the tab key?

j.



[ Reply to This | # ]
Use CDPATH to access frequent directories in bash
Authored by: TheSpoonman on Mar 21, '05 11:01:57AM
Possibly. Check out Bash Programmable Completion.

---
Answering the age-old question: which is more painful, going to work or gouging your eye out with a spoon?
www.workorspoon.com

[ Reply to This | # ]

Use CDPATH to access frequent directories in bash
Authored by: afb on Mar 21, '05 02:10:28PM

The super-ultra-cool zsh does this very well. :-)

---
now is the time for all good cows to come to the aid of their pasture.



[ Reply to This | # ]
Use CDPATH to access frequent directories in bash
Authored by: allanmarcus on Mar 21, '05 02:03:22PM

I set my CDPATH var and it's great! Tab completion worked out of the box. Great tip.



[ Reply to This | # ]
cdable_vars is also nice
Authored by: clh on Mar 21, '05 08:16:26PM
Check out the bash command shopt -s cdable_vars

From the man bash page:

cdable_vars

If set, an argument to the cd builtin command that is not a directory is assumed to be the name of a variable whose value is the directory to change to.

With this set, if I give the following bash command:

export d="/Users/chap/Desktop"

I can then simply type

cd d

to change to my Desktop directory.

I put the shopt command and the various export commands in my .bashrc file.

[ Reply to This | # ]

cdable_vars is also nice
Authored by: aggie4life on Jun 14, '09 11:10:49AM

Not to change the purpose of the thread but could someone explain the difference between .bash_profile, .bashrc, and .bash_aliases. It seems like you could just "put all your junk" in one of them and be done with it, then there's two less files you have to worry about.



[ Reply to This | # ]
cdable_vars is also nice
Authored by: aggie4life on Jun 14, '09 11:19:07AM

Addition to my post above: my point reference is an informed user, but not someone who makes a living in the unix world.



[ Reply to This | # ]