4

I'm using mv to move a huge folder from one hard drive to another and I noticed that it waits until the end to delete files it has successfully moved. Is there a reason for this? Is there a way to make it delete files as it goes or is there a different program that does this?

The problem with waiting until the end is that if mv is abnormally aborted it will take a lot longer to resume moving because each file in the destination directory will either need to be overwritten or compared against the one in the source directory. In addition if each file is deleted after a successful copy then space is freed'd up while moving which is important for emergency purposes if one hard drive is about to run out of space and a program is about to try and use more space than available and you have a flash drive or another hard drive available to prevent the already running program from failing.

The command I used was this:

mv -v -t /dst /src/folder
  • 3
    rsync --remove-source-files /dst/ /src/folder/ will do what you want. Test it properly; rsync is sensitive to presence or absence of trailing slashes. Also, the source directories (now empty) will remain, so you have to clean them up manually later, but empty directories don't take a lot of space so this 2-step thing is still better than mv for your stated purposes/problems with mv. –  Jun 20 '20 at 11:02

1 Answers1

5

That is the expected behaviour. mv should not remove whatever you asked it to move before the move is completed.

Note that you are moving a directory so mv waits until the end of the directory. If mv had been told to remove a list of files/directories, it would wait for each file/directory to be copied, for example.

I don't think there is anything you can do about it (with mv).

  • The wildcard would work if in the directory there are no folders in the folder and if there are less files than the shell arguments limit. Also the folder itself will not get removed until the end regardless of when it deletes the files and folders inside of it. – Mr. Chem Question Jun 19 '20 at 23:11
  • @Mr.ChemQuestion the folder itself will not ever be removed with the command I wrote... Anyway, the OP seemed concerned about files. But it is true that the command will not work (and return an error) if the expansion of * is too long for the shell to handle. There are many things to consider about it so I will remove the command. Thanks. – Eduardo Trápani Jun 19 '20 at 23:30
  • It's hardly the expected behaviour as in the manual it says Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY. rename means it deletes immediately. – holdfenytolvaj Jun 05 '22 at 03:58
  • @holdfenytolvaj the OP is moving a file between disks. move here means copy over to the destination device and only then removing the source. It would risk information loss otherwise. You can test it yourself, create a big file, move it to another device, or over the network and monitor (a simple ls would do) the destination and source. – Eduardo Trápani Jun 05 '22 at 14:17
  • ...Hi Eduardo, I know this is happening, I agree that this is expected for one file. I just run into the case to mv files on my own hard disk and for some reason it did not move one-by-one but started to copy the whole directory structure (and I guess wanted to delete afterwards). Now the problem was I did not have enough space for this (only if it does delete-as-move and not remove-after-copy). And I was overly disappointed in mv because of this behaviour as it is not expected but it is nevertheless it is how it works. – holdfenytolvaj Jun 06 '22 at 19:59