Skip to content

Instantly share code, notes, and snippets.

@ChristopherA
Created November 10, 2017 01:57
Show Gist options
  • Save ChristopherA/eba15436498c61e0ea00f6964188b8d0 to your computer and use it in GitHub Desktop.
Save ChristopherA/eba15436498c61e0ea00f6964188b8d0 to your computer and use it in GitHub Desktop.
Atomic mv

You should never assume that a mv command is atomic. Use the link dance:

ln -f extant new_link && rm -f extant

The hard link operations are atomic.

(The three step is when you want to save the old one:

ln -f extant new_name && ln -f new_file extant && rm new_file

... so you temporarily have two hard links on the old file (to its new_name), then you have two links to the new file (to it's target name) and then, finally, you've removed the one extra link).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment