Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've just tried to move a big tree from one disk to another, which was too small. Now, I'm left with something I can't really understand - it looks like some files were indeed moved away from the source tree while others were just copied. This is most probably not true, and I'm just overlooking something, like I did when I check the free place on the target disk. :D

The command was simply mv source-dir destination-dir, where both dirs reside on different disks. I'm using mv (GNU coreutils) 7.4. Nowhere in the man page I found the answers to the following questions:

  • may truncated files get created?
  • in case of errors, will anything in the source tree get deleted?
  • how to recover (in an easy and fast way)?
share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

I think the intro to the info page answers all your questions:

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.

Recovery should not be a problem, since the data is preserved. I do not know how much it tries to be atomic (mkdir for example is; source inspection needed) or if that's in its domain at all (vs. the kernel's), but there could be corruption if the interruption happens when it is already deleting the source. At that point you would already have a good copy in the target directory, so a simple byte size check could do the trick.

EDIT: I did some testing with huge files (a few G) created with dd, but with no conclusive results. Never managed to get only a few of the sources copied — I either cancelled the whole operation (being too fast) or afterwards not getting any chance at all due to disk caching and maybe good filesystem design (my ^C always landed after the move succeeded).

share|improve this answer
1  
Concerning the info page, do I understand it right that for each command line source directory, it gets removed iff the corresponding copy succeeded? I mean that mv src1 src2 dst works in this respect similar to mv src1 dst; mv src2 dst and each single mv src dst is like cp -a src dst && rm -rf src? –  maaartinus Jul 13 '12 at 22:38
1  
That's how I understand it too (just your mvs would be mv && mv). Since cp -a is recursive already, I doubt it applies to individual files and directories found while traversing the tree. Will do some tests later. –  lynxlynxlynx Jul 14 '12 at 8:32
add comment

When mv is moving to a different filesystem, it firsts copies all data to the new filesystem. Once the new data is copied, it removes the old data. This means that if the move is interrupted of failed, the source location should still contain all of the data.

As far as recovering, only the destination data should be corrupt. Instead of using mv to again, just use rsync and then rm manually

rsync -av source-dir/ dest-dir/
rm -rf source-dir
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.