For some reason, copying files from my phone over the MTP fuse interface sometimes results in corrupt files, missing their last few bytes. I want to remove each file on successful transfer, but not remove them if there was a problem. The mv command doesn't have a --verify option. I could write a short script which copies, checks, and removes, but I'm wondering if there's a more elegant existing command which can handle this?

As a bonus, it'd be nice to specify both checksum match and success from an external verification command, in this case jpeginfo -c. I think the short reads are random occurrences, but I haven't really tested that the bad file isn't actually cached that way (or otherwise would be read incorrectly in the same way twice). So, something like mv --verify --verifywith='jpeginfo -c' would be ideal (where jpeginfo -c is a command that tests JPEG files for correctness and which I know will return an error on these particular truncated files).


rsync can do this with --remove-source-files. From man rsync (emphasis mine):

This tells rsync to remove from the sending side the files (meaning non-directories) that are a part of the transfer and have been successfully duplicated on the receiving side.

I'm not sure how it verifies successful duplication, other than that it will not remove them if an error occurred during the process. It's quite possible that that is all it does.

If the interface is silently corrupting files, however, I think even copy/check/remove will not really fix your problem, since even the check might produce false results.

Since you mention MTP and a phone, I'm going to guess you're using Android. If you'd consider an alternative FUSE interface, you might like to try adbfs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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