kerravonsen: (Default)
[personal profile] kerravonsen
Just saw on FreshMeat someone was announcing a utility called Working Directory, and I thought to myself, haven't these ignoramuses heard of the CDPATH environment variable?

To explain: assuming you know what a directory is, and indeed, what a working-directory is (also known as the current directory or the current working directory), what the "Working Directory" utility does is enable you to store your most commonly used directories as aliases, and change directories to them by using the said aliases (of which there are only ten). The aliases are cryptic, so you have to keep listing them in order to remember which alias applies to which directory. The CDPATH environment variable, on the other hand, enables you to meaningfully shortcut to your most commonly used directories, with no number limits, though it has other limitations. The reason I wonder why they aren't using CDPATH is that "Working Directory" is designed for the bash shell, and bash recognises CDPATH just like most shells do.

So what is CDPATH and how do I use it? CDPATH is an environment variable (as I mentioned before) which you set in your initialization file (such as .bashrc or .profile or .kshrc or .zshrc depending what shell you use). What you put in it is a list of colon-separated directories; these are the full path names of the directories above the ones you work in most commonly (plus the special shortcuts '.' and '..') Why above? Because the way it works is this: you type

	cd dirname

and the shell checks the list of directories in CDPATH to see if any of them contain a sub-directory called "dirname", and the first one it finds is the one it will take you to. Thus it makes sense to ensure that the first directory in the CDPATH is '.' (for the current directory) otherwise it might behave strangely. The '..' shortcut stands for the directory above the current one, which can be very handy.

Using CDPATH makes sense if you tend to spend your time in a particular set of directories, and these directories don't have directory names in common. Say, for example, you do work in the directory /files/projects/fred, which has sub-directories in it "widget", "gadget" and "dongle", and your home directory has directories in it called "Mail", "stuff", "src" and "junk". You could set your CDPATH to be

export CDPATH=.:..:/home/fred:/files/projects/fred

Then if you went "cd dongle" it would take you to /files/projects/fred/dongle, and if you went "cd stuff" it would take you to the "stuff" directory in your home directory. Mind you, since this takes you to the first matching directory it finds, the above CDPATH would ensure that if you were in the /files/projects/fred/dongle directory, and that directory had a sub-directory called "src", it would take you to /files/projects/fred/dongle/src and not to /home/fred/src because "." (the current directory) comes before "/home/fred" in the list. That is why you must always ensure that the first directory in your CDPATH variable is ".", or you will end up in strange places you don't expect.

But I still think that CDPATH is cooler and more useful than this "Working Directory" thing. And it's built-in!

Last night I got home and found not one, not two, but three packages from Amazon on my doorstep. It seems that they were all posted in different bunches, but just arrived at the same time.

So what were my bookie treats? "Rathlyn's Flight", an SF Romance which got some decent reviews on the Amazon site, but now, looking at the blurb, alas looks as if it falls more into the "bodice-ripper" genre than I like. We shall see, after I finish reading "Time Future" by Maxine McArthur which I finally started reading a couple of weeks ago (I had bought it at the Natcon last year when I listened to a panel which included the author and it sounded like the main character would be a good female protagonist, but I haven't been in the mood for it until just recently. There are other novels from that Natcon bunch which are still languishing also). Other books in the bundles: "Devlin's Honor", the sequel to "Devlin's Luck" (the first one was fairly good, so I got the sequel), "The Tribune" (a historical which also got good reviews) and the DVD "Samurai Jack" (yes, I actually got the DVD, which basically has the first few episodes of the series). I was going to jump in and watch the DVD as soon as I unwrapped it, but before I did, I unwrapped the third parcel and dropped everything: it was "Irresistable Forces" which is the anthology which contains the Lois McMaster Bujold story "Winterfair Gifts" which is the story about Miles's wedding -- so I sat down and read that story in full. (grin) It was nice to have the PoV switch -- this one is told from the PoV of Armsman Roic, and we smile sympathetically at him in his confusion when he runs headlong into things which he didn't know about his employer... (evil grin).

Date: 2004-02-26 12:38 am (UTC)
ext_50193: (Default)
From: [identity profile] hawkeye7.livejournal.com
I use CDPATH all the time – so much so that I created a special version of cd for Win32 that has the feature. It's particularly useful for navigating our project hierarchy, as environment variables can be part of the CDPATH. Java, in particular, seems to demand that the source files be buried at least two levels deep.

Profile

kerravonsen: (Default)
Kathryn A.

Most Popular Tags

January 2026

S M T W T F S
    1 23
45678910
11121314151617
18192021222324
25262728293031

Expand Cut Tags

No cut tags

Style Credit

Page generated Feb. 8th, 2026 12:05 am
Powered by Dreamwidth Studios