Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

6 Answers

up vote 29 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.

share|improve this answer
    
This assumes that the source and destination are on the same volume. –  Ignacio Vazquez-Abrams Mar 18 '11 at 10:00
    
Interesting! Also, if interrupted midway a file move, the source file will remain as you said, but the destination will be a partial file? –  invert Mar 18 '11 at 12:20
4  
@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  
Wrong. If you move dir from one fs to another mv /fs1/dir /fs2/ and you interrupt, /fs1/dir/ will stay here completely. /fs1/dir is removed only when the move is complete. –  user263131 Oct 13 '13 at 9:30
    
user263131 is right. Run strace mv /fs1/dir /fs2/ - the very last thing mv does is calling unlinkat on all source files at once (not one by one as they are copied). –  Jakob Jul 5 at 9:58
add comment

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

share|improve this answer
add comment

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
share|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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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. I don't have enough rep to upvote.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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