UNIX atomically swap or replace directories? UNIX atomically swap or replace directories? unix unix

UNIX atomically swap or replace directories?


Linux provides the renameat2 system call which with the RENAME_EXCHANGE flag will swap 2 objects:

renameat2(AT_FDCWD, "dir1", AT_FDCWD, "dir2", RENAME_EXCHANGE);


I don't think that your assertion above that the second command will fail if the first fails is true. If mv dir1 dir1_backup fails, then dir1 will still exist, and mv dir2 dir1 will make dir2 a subdirectory of dir1. Also, since you are passing -f to rm in the third command, it will silently fail to delete a non-existant directory.

For reference, three bash operators (assuming bash here) and what they do:

  • ; simply executes the following command after the first exits, regardless of the exit status.
  • && executes the following command only if the previous command exited cleanly (ie, had an exit status of 0.)
  • || executes the following command only if the previous command exited uncleanly (ie, had a non-zero exit code.)

If you want each command's execution to be dependent upon the previous command's successful execution, then you might want to consider something like the following:

    mv dir1 dir1_backup && mv dir2 dir1 && rm -rf dir1_backup

If the moving of a directory is a failure mode you expect, then it may be reasonable to test the return value of the move or to test for the existence of the expected directory before proceeding to remove the old directory contents. If the test fails, then move the old directory contents back. The overall operation will have failed, but at least you will not be in an invalid state. For example:

    mv dir1 dir1_backup && \    mv dir2 dir1 && \    rm -rf dir1_backup || mv dir1_backup dir1

Since we know that && executes the following command only if the previous command exits successfully, mv dir2 dir1 will only execute if mv dir1 dir1_backup succeeds.

Further, rm -rf dir1_backup will only execute if the move of dir2 to dir1 succeeds. However, if the last exit code (the code returned by mv dir2 dir1 if it has failed) is non-zero, the || operator causes mv dir1_backup dir1 to execute.

Hope that helps! Let me know if it needs clarification.


On Linux you might be able to get away with bind mounting the new directory on top of the old. However you would still have issues with processes that had an open handle on a directory inside the old directory(eg their current working directory).

mount --bind dir2 dir1

Similar tricks would allow you to gain access to the old directory for cleanup purposes.

If this filesystem is exported over NFS then you would probably need to ensure the export options were set to allow traversing file system boundaries and do the bind mount on the server.

Other *nix have similar features but they aren't standardised.