I would like to change the target of symbolic link from within a bash script. The problem is that the symlink is quite important (it's /bin/sh, namely) and I would to do it in fashion that:
I thought about two methods. Either using plain ln:
ln -fs /bin/bash /bin/sh
or using mv:
ln -s /bin/bash /bin/sh.new
mv /bin/sh.new /bin/sh
Which one will suit my needs better? Is there any possibility that one of them would try to replace the symlink target instead of symlink itself?
Renaming (mv) is an atomic operation; creating a new symlink is not (delete old symlink; create new one). So you should use mv:
$ ln -s new current_tmp && mv -Tf current_tmp current
Here's a blog post discussing this. Also, if you're worried about what will happen, why not try it on a non-critical symlink first?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With