n8blog
distraction in action

This is a little script that calculates the shortest relative path from a directory to one or more arbitrary paths. For example:

[n8gray@golux photos]$ pwd
/Users/n8gray/web/Sites/photos
[n8gray@golux photos]$ relpath . ~/Pictures/
../../Pictures
[n8gray@golux photos]$ relpath /usr/local /usr/local/bin/nedit
./bin/nedit
[n8gray@golux photos]$ relpath / /
./
[n8gray@golux photos]$ relpath . ..
../
[n8gray@golux photos]$ relpath . /usr/local ~/bin ../photos/Cats/index.html
../../../../usr/local
../../bin
./Cats/index.html

Caveat Emptor: Symbolic links are resolved when you give a relative path but not when you give an absolute path. I intend to make this more deterministic and controllable in the future.

Get it here. Python 2.3 required.

  Comments:

1. Jefe replies:

Too bad OS X can’t get their S#%T together and just work out the symlink thing once and for all.

Having both Aliases and symlinks is stupid, esp when Aliases just show up as zero length files (under HFS+ at least) and can’t be followed from the command line.

Then, unless it’s my imagination (or a bad version of OS X bash), it seems like relative paths in OS X symlinks only work HALF the time. The other part of the time, you wind up with the absolute path you never wanted and a bald front tire.

Jefe

2. n8 replies:

Yeah, it’s a shame about the alias/symlink thing, but each serves a different purpose. Aliases are about 1/2 hard link and 1/2 symlink. If a target file is moved, they still point to it, but if it’s deleted and another is put in it’s place then they’ll point to the new file. Symlinks, OTOH, always point to a given path, whether or not the target is moved. In fact, the “target” might not ever have existed!

It would be nice if aliases could be used from the command line, though.

BTW, I should mention that I’ve never had any unexpected behavior with relative paths in symlinks under OS X.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please type this word with the letters reversed: live