Lost data after cancelling moving folder

I was tried to move 30GB folder to my external exFat drive on Ubuntu 20.04.2. I see the some subfolder on exFat during moving operation. I cancelled moving operation because it looks take a lot of time. After cancellation subfolders gone on exFat, also they are missing on Ubuntu side. I tried to recover exFat on Windows machine but there was no evidence for these sub folders.

What was the problem?


In principle, it works as following. On a move operation, each file in the source is deleted as soon as it has been successfully copied to the destination. When you interrupt a move operation during the transfer of a (typically a larger) file, the destination file is incomplete (this is in my opinion unfortunate behaviour of linux - rather, the incomplete copy, i.e., a corrupt file, should be deleted), however the source file is kept.

Second consideration is that Linux extensively uses RAM to buffer file transfer operations. That means that content is first cached into RAM memory, and only then physically written out with some delay on the destination disk. Because of this mechanism, the copy appears to be finished earlier than it is in reality. Once, all files have been read to the cache, the copy is considered finished. The operating system will then, in the background, continue writing the files to the disk.

That you have corrupted files leads me to suspect that you may have removed the exfat drive too early. You may have physically removed your drive after the system told you that your move operation was cancelled. The system tells so, but in reality, physical write operations may still have been ongoing in the background based on the caching I explained above.

This is the main reason why you always need to unplug your drives after having disconnected them through the software. If you first try disconnecting through the software, the system will warn you if physical removal is not yet safe, flush the cache and let you know when it is safe to remove the drive.

A recommendation: prefer to copy rather than move a large set of files first, then delete the source once the copy is complete. If you cancel, all source files are still there, untouched.