What happens if the Linux
mv command is interrupted? Say, I'm moving a whole directory to some other place and interrupt it while it's moving. Will the source directory still be untouched?
If you move a directory on the same file system, you only move the directory entry from one location in the file system to another one.
mv /source/dir /target/dir will delete the directory entry of
/source and create a new one in
/target. That's done by one atomic system call (i.e., uninterruptable). The inode containing the directory entries of
dir as well as the actual content of the directory itself is not affected.
If you move the directory from one file system to another, all of the files are first copied to the new file system and then unlinked from the original one. So if you interrupt
mv while it’s copying, you may end up with two copies of some of the files – at the old location and at the new one.
The GNU implementation iterates over the arguments on the command line, attempts to rename first, and, if that fails, recursively copies and then recursively deletes the source. So
mv a b c/
will delete a before copying b, and will not start deleting anything in a before the destination copy is complete.
Note that this applies to the GNU implementation only.
To clarify: if a is a directory containing d and e, and b is a file, the order will be
- create c/a
- copy a/d -> c/a/d
- copy a/e -> c/a/e
- delete a/d
- delete a/e
- delete a
- copy b -> c/b
- delete b
You move one directory, interrupt the move, and the original directory will stay intact:
$ mv a b/
If you move multiple directories, each one will be intact on either the source or destination, depending on when you interrupted:
$ mv a b c/
How I got my answer:
$ mv --version mv (GNU coreutils) 8.21 $ info mv ... It first uses some of the same code that's used by `cp -a' to copy the requested directories and files, then (assuming the copy succeeded) it removes the originals. If the copy fails, then the part that was copied to the destination partition is removed. If you were to copy three directories from one partition to another and the copy of the first directory succeeded, but the second didn't, the first would be left on the destination partition and the second and third would be left on the original partition.
As a test, I copied a large folder to an NFS directory, interrupted, and the number of files in my source large folder stayed the same, and partial contents were left on the NFS directory. I used "find . -type f | wc -l" to verify.
Looks like Simon's answer is correct.
The accepted answer is definitely wrong about moving between file systems - a fact that saved me a lot of trouble a couple of times already. When moving a directory that containes subdirectories, no file in a subdirectory will be deleted before the whole subdirectory has been copied. This is, btw.the actual meaning of "object by object" - a subdirectory IS an object (file) and thus its integrity has to be preserved by a complete copy at the destination before anything can be deleted. So Simon's answer appears to me as the correct one.
Definitely no. The move is made object by object. Hence, the object moved to the destination up to the point of interrupt shall not exist in the source any more.
If mv was issued for a large file (between different) and it has been interrupted then the source will be intact. On the target you will see an incomplete file up to the point of interruption.
You can however restore the mv with the same command and the process will continue.