0

I'm looking for an (preferably portable) way to create 'short' relative symbolic links. That is for two absolute file names target and name I want to find an equivalent and short relative file name reltarget such that after

ln -s $target $name

name points to the same location as after

ln -s $reltarget $name

.

Some examples:

target="/usr/bin/program-1.2"; name="/usr/bin/program";
=> reltarget="program-1.2"

target="/usr/lib/program-1.2/bin/program"; name="/usr/bin/program-1.2";
=> reltarget="../lib/program-1.2/bin/program"
Uwe Kleine-König
  • 3,426
  • 1
  • 24
  • 20

1 Answers1

1

This will do it if you have Python 2.6 or newer on your system; you may need to modify the quoting if launching from another shell than bash.

ln -s "`python -c "import os.path; print os.path.relpath('$target','$name')"`" "$name"

Inspired by this.

Community
  • 1
  • 1
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294