Flip Symlinks Atomically
… which is something that comes in handy if you need to deploy a new release of software.
What's the deal?
Think of a structure like this:
/opt |-- application-1.0.0 |-- application-1.1.2 `-- current -> application-1.0.0
With every new release the 'current' symlink has to point to the new release.
And how do you do this?
rm current && ln -s application-1.1.2 current
That works usually very well, but that's not atomic and there is a minute time frame where the link is missing which might cause issues.
The better alternative is:
ln -s application-1.1.2 current.new tree -d -L 1 /opt /opt |-- application-1.0.0 |-- application-1.1.2 |-- current -> application-1.0.0 `-- current.new -> application-1.1.2 mv -T current.new current tree -d -L 1 /opt /opt |-- application-1.0.0 |-- application-1.1.2 `-- current -> application-1.1.2
'man mv' suggests to see also
'man 2 rename'
we learn: “if newpath refers to a symbolic link the link will be overwritten.” And that's exactly what we want.