What is the absolute fastest way to swap a directory and all its contents?

Rino Bino asked:

I have a regular task that I need to swap out a live/accessed dir with a new version of it.

  • There is a live dir with resources in it: dir/
  • Goal is to swap this out with dir_new/ and cause as little disruption in access to the dir.
  • The content is live/accessed. Stopping access to the directory from end users is out of scope. I would just do this during a downtime window otherwise.

Here’s how I do it now:

mv dir dir_old && mv dir_new dir

This works fine, but I’m not sure if any experts have a faster/more reliable way?

  • The filesystem is xfs
  • The "new" dir cannot have ANY content from the existing dir in it, so moving in place, dealing with files didn’t seem like it would work.
  • Perhaps rsync with a --delete option? However a requirement is to change "everything" at once in the dir. In other words I can’t have a mix of files from the new and old mixed together.

My answer:


Neither of the directories should be named dir. Rather, dir should be a symbolic link to the directory containing the files you want to be used. You can then change the link atomically whenever necessary.

For example, name your directories e.g. dir_20200917, dir_20201015, etc. Then link to the one which should be live:

ln -sf dir_20201015 dir

Note that -f here will replace the existing symlink, if any.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.