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?

link|improve this question

63% accept rate
feedback

4 Answers

up vote 23 down vote accepted

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 the files are transferred one-by-one (as Ignacio mentions in his answer), i.e. if you interrupt the mv the already transferred files are removed from the source directory.

link|improve this answer
This assumes that the source and destination are on the same volume. – Ignacio Vazquez-Abrams Mar 18 '11 at 10:00
@Ignacio: Yes you are right. I will add a remark. – bmk Mar 18 '11 at 10:05
Interesting! Also, if interrupted midway a file move, the source file will remain as you said, but the destination will be a partial file? – wez Mar 18 '11 at 12:20
2  
@Wesley: No, there will be no partial file. If the system stays up (e.g., you hit ctrl-C), it will be removed automatically. If not (e.g., power loss), the partial file may be left somewhere inaccessible on the destination disk, but should be cleaned up by the next fsck (which will most likely run automatically on reboot, since the disk wasn't unmounted cleanly). – Dave Sherohman Mar 18 '11 at 12:42
+1 for the added part. – Johan Mar 18 '11 at 15:41
feedback

No. mv operates object by object, so objects that have already been processed will be removed from the source.

link|improve this answer
feedback

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.

link|improve this answer
feedback

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
link|improve this answer
Can you provide a source for this? Other answerers are saying a source file is deleted immediately after it is copied to a different fs (i.e. not all copied then all deleted). – Puddingfox Mar 18 '11 at 18:02
Experience. I've added an example to make it more clear how my answer and the others are consistent. If you only list individual files then indeed each file will be deleted immediately. – Simon Richter Mar 18 '11 at 18:08
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.