Replace Symbolic Links with Files

If i understood you correctly the -L flag of the cp command should do exactly what you want.

Just copy all the symlinks and it will replace them with the files they point to.


Might be easier to just use tar to copy the data to a new directory.

-H      (c and r mode only) Symbolic links named on the command line will
        be followed; the target of the link will be archived, not the
        link itself.

You could use something like this

tar -hcf - sourcedir | tar -xf - -C newdir

tar --help:
-H, --format=FORMAT        create archive of the given format
-h, --dereference          follow symlinks; archive and dump the files they point to

For some definitions of "easy":

#!/bin/sh
set -e
for link; do
    test -h "$link" || continue

    dir=$(dirname "$link")
    reltarget=$(readlink "$link")
    case $reltarget in
        /*) abstarget=$reltarget;;
        *)  abstarget=$dir/$reltarget;;
    esac

    rm -fv "$link"
    cp -afv "$abstarget" "$link" || {
        # on failure, restore the symlink
        rm -rfv "$link"
        ln -sfv "$reltarget" "$link"
    }
done

Run this script with link names as arguments, e.g. through find . -type l -exec /path/tos/script {} +

Tags:

Linux

Bash