Is it better to always copy and delete, rather than move?

18

5

Generally speaking, I find myself panicking when I realise that if I cancel a file move, it could cause the target or source to be incomplete. This question applies to Windows and Unix-based platforms. I can never remember exactly how the move command works in either case. For example, if you're moving a directory; does it copy the entire directory, and then delete it after, or does it copy and then delete each file individually?

I always realise, after typing something like mv verybigdir dest, that I perhaps should have typed cp -R verybigdir dest  &&  rm -R verybigdir (where the && operator proceeds to the next command only if the first was successful) -- or is this pointless? What happens, exactly, when I press Ctrl+C half way through a move? Likewise, what exactly happens on Windows when I press the cancel button?

I can't count the number of times I've moved something (the last time was when using svn) and had two directories, with split contents. I guess the answer is difficult, because not all applications move groups of files in the same way.

Nick Bolton

Posted 2010-01-12T18:44:58.917

Reputation: 2 941

16Whatever you do, just don't delete first. – mtone – 2010-01-12T20:29:43.747

@monotone Hilarious +1 – Nick Bolton – 2010-01-12T20:34:47.497

Answers

10

On Windows, moving to the same drive and partition will act just like Unix's mv command and rename the folder or change its parent. However, if you were to move it to another driver or partition, it would copy and delete file by file, hence why it is more efficient to use a tar file or a zip file with no compression to move files faster across partitions and hard drives. If you were to cancel it, it would simply stop where it is. I would believe the same is true for Unix but I did not experiment enough with it to be 100% sure. It is just a matter of changing the inode, but if it is on another partition or drive, then it needs to be copied to sectors on that partition or drive. If you were to cancel it during the transfer, it would already have moved some files, and the file that was being transferred will see it's destination file removed and the source file kept (transaction cancelled, but your original file still exists).

Update: If you were to cancel a move and wanted to resume it, just re-issue the order to move. It might warn you that the destination folder already exists, but the files will not be overwritten (unless they existed before the original move, or got added in between the two move orders) since as soon as they are transferred, they are deleted from the source (if on a different partition or drive).

Wolf

Posted 2010-01-12T18:44:58.917

Reputation: 331

"On Windows, moving to the same drive and partition will act just like Unix's mv command and rename the folder or change its parent." - When using Windows Explorer that's not true. It does a lot of crap, it takes ages, and you may end up with content split across two directories, see this answer to a question of mine. – maaartinus – 2011-03-11T08:49:59.047

@maaartinus Actually, the way Windows Explorer works is it digs down recursively in the folders rather than changing the parent node. You are right though for Windows Explorer, it is not just an atomic move. I assumed command line was being used, I should have asked. – Wolf – 2011-03-17T15:00:27.677

Ah, I hadn't thought about compression. – Nick Bolton – 2010-01-12T20:34:11.770

You don't really need to compress, which can require a lot of time. Sometimes a compression level of 0 (storage) can do the job to move big folders across drives much faster. – Wolf – 2010-01-12T20:39:05.007

11

Even on a perfectly stable computer that never fails: if you care about timestamps, then mv is better than plain cp.

(cp -a will preserve the timestamps for you, and I assume something similar exists on Windows).

Arjan

Posted 2010-01-12T18:44:58.917

Reputation: 29 084

1@Marcin, they're there for a reason, if that's what you're asking ... – macek – 2011-09-16T14:06:22.443

1Aha, timestamps. I had not considered that! +1 – Nick Bolton – 2010-01-12T20:26:52.333

Are timestamps even important honestly... – Marcin – 2010-01-13T04:51:55.203

10

No.

Explanation:

mv verybigdir dest

renames verybigdir to dest. This is an atomic operation, i.e. can not fail halfway through.

If dest is on another device, mv will copy first, then delete the old version. This is not an atomic operation. If it fails, you may have only a partial copy of verybigdir in dest, but verybigdir will still be complete.

Yes, other applications may move files differently.

Timo Stamm

Posted 2010-01-12T18:44:58.917

Reputation: 101

2Basically, mv handles each argument in a "cp && rm" way... mv verybigdir/* dest splits the mv of verybigdir into lots of separate move operations. Always remember that unlike on Windows, a Linux program/tool doesn't get to see that "*" there, it's expanded by the shell. – Jürgen A. Erhard – 2013-06-24T20:56:22.327

2Actually, I think mv on Linux copies/deletes each file or directory individually, rather than copying the entire tree to the destination then deleting the entire tree from the source...so if you cancel halfway through, you'll end up with some files in both places, and neither directory will be complete. – rob – 2010-01-12T19:38:52.617

3@rob: No. Directories are files like any other, and are treated the same way. The behavior you are talking about is achieved with mv verybigdir/* dest. – dmckee --- ex-moderator kitten – 2010-01-12T20:30:42.323

4

On windows i always copy and delete instead of move. I was once moving files and this is where i first noticed a bad stick of memory. When moving the file it stopped in between and i got an error so i checked the source folder and the file was gone then i checked the destination and there was a corrupt file. This happened more frequent with bigger size files and most of them were downloads which took hours so id recommend copying and then deleting. Save yourself time in the beginning it will pay off in the end.

at.

Posted 2010-01-12T18:44:58.917

Reputation: 128

3

On unix, on moves that don't cross filesystem boundaries, mv does not copy the data: it just updates the inode database in various directories. This is much faster than cp on large files.

Further, using mv across filesystem boundaries just silently invokes a copy and delete mechanism.

So I think you should prefer mv.

dmckee --- ex-moderator kitten

Posted 2010-01-12T18:44:58.917

Reputation: 7 311

"a copy and delete mechanism" could be interpreted as either "copy each file, then delete it" or "copy all the files, then delete all of them". – j_random_hacker – 2019-04-18T14:38:56.753

Nicely summed up. – Nick Bolton – 2010-01-12T20:33:21.227

2

In Windows, at least, move is simply a more automated Copy&Delete. I believe mv moves each file individually, meaning ^c-ing won't lose any files, you'll just end up with your files split between two places - unlike Windows Explorer, which will un-move everything when it cancels.

My solution to this is: Never move unless I'm sure I want to move it.

Phoshi

Posted 2010-01-12T18:44:58.917

Reputation: 22 001

1

If you are using ACLs on the source filesystem, but not on the destination, mv on Linux will copy the source and then - because of the inability to set the ACLs on the destination - stop. So you end up having the file on both sides. The is no switch in mv to prevent that behaviour so in this case cp && rm is preferable.

uli42

Posted 2010-01-12T18:44:58.917

Reputation: 46