A wise and frugal government, which shall restrain men from injuring one another, which shall leave them otherwise free to regulate their own pursuits of industry and improvement, and shall not take from the mouth of labor the bread it has earned. This is the sum of good government. - Thomas Jefferson
Saturday, 28 January 2012 00:53 | Author: Judson |
I have been trying to find a way to replace symbolic links with the files that they are pointed to. It turns out the solution required a little script:
#!/bin/bash
echo Processing $1
dir=$(dirname "$1")
reltarget=$(readlink "$1")
if [[ $reltarget == /* ]]; then
abstarget=$reltarget
else
abstarget=$dir/$reltarget
fi
rm -fv "$1"
cp -afv "$abstarget" "$1" || {
# on failure, restore the symlink
rm -rfv "$1"
ln -sfv "$reltarget" "$1"
}
Replace a link with its target by using the script like this:
ReplaceSymLink.sh file1
or in combination with the find command like this:
find -type l -exec ReplaceSymLink.sh {} \;
(assuming that you have placed the script somewhere that is in your executable search path)