2

I'm migrating a file system and I need to update a few thousand symbolic links that'll break as a result of the migration. My plan was to find all the affected links and build a script that'd process each link. For example:

ln -nsf "$(readlink "./somelink1" | sed "s|oldpath|newpath|") "$(echo "./somelink1" | sed "s|oldpath|newpath|")"
ln -nsf "$(readlink "./somelink2" | sed "s|oldpath|newpath|") "$(echo "./somelink2" | sed "s|oldpath|newpath|")"

I'll need to run the script as root, however, it looks like that'll change the ownership of the all the links to root:root. Is there a way I can update the links and keep the existing UID:GID? I'm sure I can capture the ownership while the links are enumerated and add a command to chown each link, but I'm wondering if there is a better way to retain ownership?

user
  • 83
  • 1
    AFAIK the only time symbolic link ownership really matters is when it's in a directory with the sticky bit set and that this bit prevents users deleting files they do not own. Not answering your question, mind, but it may help your perspective – Chris Davies Jan 07 '22 at 22:01
  • A possible alternative approach, but I don't know if it's actually easier: tar all the links, use python tarfile to edit the tar, untar. At least with GNU tar, untarring preserves the ownership and timestamp of symlinks. – Gilles 'SO- stop being evil' Jan 07 '22 at 22:16

0 Answers0