Commit 252e6329 authored by Glen Mével's avatar Glen Mével

`collapse-link`: replace a symbolic link by its contents

parent 1f0e3720
#!/bin/sh
# USAGE:
# collapse-link LINK1 LINK2 …
# This script replaces any symbolic link given as argument by its target;
# the target is pulled (moved) from its original place, not copied.
set -e
for link ; do
# if the argument given is not a symlink, ignore it:
[ -h "$link" ] || continue
# compute the target:
dir=$(dirname -- "$link")
target=$(readlink -- "$link")
case "$target" in
/*) abstarget="$target";;
*) abstarget="$dir/$target";;
esac
# mv refuses to replace a symlink by a directory:
[ -d "$abstarget" ] && rm -fv -- "$link"
# replace the symbolic link by its target:
mv -Tfv -- "$abstarget" "$link"
# on failure, restore the symlink:
if [ $? -ne 0 ] ; then
[ -d "$abstarget" ] && ln -sfv -- "$target" "$link"
fi
done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment