2

Please, I want to change the symlink to the new path
My symlink's in this form

lrwxrwxrwx. 1 s01 s01 56 Mar 22 13:17 zmjppcxlpq2m.png -> /home/s01/public_html/cgi-bin/uploads/00029/zmjppcxlpq2m
lrwxrwxrwx. 1 s01 s01 56 Mar 23 17:03 znviy3civtq3.jpg -> /home/s01/public_html/cgi-bin/uploads/00029/znviy3civtq3
lrwxrwxrwx. 1 s01 s01 56 Mar 24 20:07 zoi46hh4pu32.jpg -> /home/s01/public_html/cgi-bin/uploads/00029/zoi46hh4pu32
lrwxrwxrwx. 1 s01 s01 56 Mar 23 18:22 zqt0h1gy9e4c.png -> /home/s01/public_html/cgi-bin/uploads/00029/zqt0h1gy9e4c

I transferred all files to a new portion from /home to /home2

So I want to change all symlinks to the new path. Ex: Old:

zmjppcxlpq2m.png -> /home/s01/public_html/cgi-bin/uploads/00029/zmjppcxlpq2m

I want to have

zmjppcxlpq2m.png -> /home2/s01/public_html/cgi-bin/uploads/00029/zmjppcxlpq2m

for all files. what can I do?

0

1 Answer 1

4

You can change the target of a soft link without deleting the old one by forcing ln to do so.

ln -f -s /new/location/to/link newlink

Eg:

ln -f -s /home2/s01/public_html/cgi-bin/uploads/00029/zmjppcxlpq2m zmjppcxlpq2m.png

or you can do an atomic replacement with mv:

ln -s /home2/s01/public_html/cgi-bin/uploads/00029/zmjppcxlpq2m new_zmjppcxlpq2m.png
mv new_zmjppcxlpq2m.png zmjppcxlpq2m.png

To change all links in some directory, try with something similar to:

find /dir/with/symlinks/ -lname "/home/s01/public_html/cgi-bin/uploads/00029/*" \
    -exec  sh -c 'ln -sf `echo $(readlink $0) | sed s/home/home2/` $0' {} \;

be careful backup all symlinks before execute any command

3
  • I did not understand,My Links in front of you, What i do, I can not set the commands
    – Mohammed OK
    Commented Mar 28, 2015 at 2:36
  • also I did not understand, Why you not use my link's In your explanation, old symlink and new symlink, Even I understand that I change, old /home/s01/public_html/cgi-bin/uploads/00029/zmjppcxlpq2m new /home2/s01/public_html/cgi-bin/uploads/00029/zmjppcxlpq2m
    – Mohammed OK
    Commented Mar 28, 2015 at 3:44
  • Yeeeeeeeeeeeeeeeeeees, we have succeeded Thank you very much
    – Mohammed OK
    Commented Mar 28, 2015 at 4:24