What happens if mv is interrupted?

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.  E.g., mv /source/dir /target/dir will delete the directory entry of dir from /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.

Tags:

Linux

Mv