Linux - updates multiple symbolic links with a single command

When cloning an environment in which symbolic links are used, symbolic links are copied (from production to clone), but they still point to the source (production) files. I want them to point to cloned files, for example:

Original:

/production/symlink1 > /production/directory/file1
/production/foo/symlink2 > /production/directory/sub/file2

After cloning (now):

/clone/symlink1 > /production/directory/file1
/clone/foo/symlink2 > /production/directory/sub/file2

I want to:

/clone/symlink1 > /clone/directory/file1
/clone/foo/symlink2 > /clone/directory/sub/file2

Is there a way to achieve this with a single command?

+3
source share
2 answers

Have you created the links yourself? If so, you can create them using the parameter -r. See man ln:

-r, - relative

          create symbolic links relative to link location
+1
source

, . , , .

. , script relink.sh :

#!/bin/bash
for link; do
    target=$(readlink "$link")
    [[ $target =~ ^/production ]] || continue
    newtarget=$(echo $target | sed -e s?/production?/clone?)
    echo ln -snf "$newtarget" "$link"
done

script, , - /production, /production /clone. , :

find /clone -type l -exec ./relink.sh {} \;
+1

All Articles