rename(2) is called by
mv, is it safe to assume the following would be atomic?
$ mv /home/me/someDir /tmp/toBeDeleted $ rm -rf /tmp/toBeDeleted
If the directories are on the same hardware partition mounted as a single filesystem, then moving something is actually just renaming it to a different path. However, if they are not, then each file inside may need to be read in and copied out, so no part of the move would be atomic. As Gilles points out, POSIX stipulates this is the case for discrete filesystems.
Excepting that, a quick check with
mv does use the
rename() system call (not to be confused with
rename, the command line utility). That would make
mving a directory atomic from a userspace perspective. The
rename() system call will throw an EBUSY error if:
oldpath or newpath is a directory that is in use by some process (perhaps as current working directory, or as root directory, or because it was open for reading) or is in use by the system (for example as mount point), while the system considers this an error. (Note that there is no requirement to return EBUSY in such cases—there is nothing wrong with doing the rename anyway—but it is allowed to return EBUSY if the system cannot otherwise handle such situations.)
man 2 rename. The connection to "atomicity" here is that you can't interrupt another process working in the directory, and another process can't interrupt this -- it'll end up with an invalid path/not found type error if you beat it in the chase.
mvthen works by copying the source tree to the destination and then removing the source tree. This is evidently not atomic.
renameis not atomic, such as certain implementations of NFS. On any “normal” local filesystem,
Both answers say essentially the same thing, but only focus on one aspect of removal.
If you have a shell whose working directory is within the renamed/moved directory tree, it will continue to see and use those files until they are actually removed. Because of this, the shell will see the files in varying states of deletion, and consequently, the rename/move (while may be "atomic" in itself) is not a atomic form of deletion from the point of view of all users of the files. It only affects users whose shell is outside the directory tree from the outset.
The shell maintains its own information regarding which directory it is in. That is because in some configurations you may change the current directory into one where you have no permissions to read the chain of directory information needed to determine the actual path, e.g., by following a symbolic link into a protected directory.