[NCLUG] Collapsing Symlinks with unix tools ?

Gabriel L Somlo somlo at CS.ColoState.EDU
Fri Aug 9 12:37:50 MDT 2002


Hi !

By an unfortunate design mistake ( :) ) I ended up with a chain of
symlinks to a file, e.g. :

	symlinkN -> symlinkN-1 -> ... -> symlink1 -> RealFile

Now, I would like to replace all these symlinks with direct symlinks
to RealFile. I'm about to write a bash function which would do
something like this:

# L is the first symlink, T is the target of L
L=symlinkN
T=$(ls -l ${L} | awk -F "->" '{print $2}')
# Track down the ultimate target, RealFile
while [ -L ${T} ]; do
  # replace L with T, and recompute the target
  L=${T}
  T=$(ls -l ${L} | awk -F "->" '{print $2}')
done
# Now, ${T} is RealFile, point symlinkN at it
rm -f symlinkN
ln -s ${T} symlinkN

Of course, this could be made recursive in order to fix *all* symlinks
in the chain, not just the first one, symlinkN.

Anyway, my question to you is this: do you know of any existing UNIX
tools, such as ln, that have flags/options which would do all of the
above automatically ? (I checked the manpage for ln itself, and
it doesn't seem to do anything like this, but what about other tools?  :) )

Thanks much,

Gabriel



More information about the NCLUG mailing list