Copy and sync
Copy operation needs two things: source and destination. The situation is simple when the destination is empty. But what if it is not? In all cases, we want to use the fact that some data already exists in the destination, and we do not want to copy them again. There are two main scenarios:
- Copy files from source to destination, so that destination becomes sum of current state and new files. I will call it simple copy.
- Copy files from source to destination and remove files that are in the destination, but not in source. This essentially means that in the end destination has exactly the same data as source. I will call it mirroring.
And here is how to do it.
Synchronize directories in Windows
Copying files is my hobby and I once found a great tool: robocopy.
It can handle both copy scenarios. I use it with PowerShell, and I recommend using $PSScriptRoot variable.
Mirror entire directory:
robocopy "K:\work" "J:\work" /e /mir
There’s a catch when mirroring entire disks: Windows is funny when it comes to the Recycle Bin directory, so exclude it:
robocopy "K:" "J:" /xd "K:\`$RECYCLE.BIN" /e /mir
Copy directory to where the script is located (do not remove anything):
robocopy "D:\work" "$PSScriptRoot\work" /e
Remember that if the script you are running like this is located in the destination directory, it may be removed when mirroring, if it is not in the source directory.
Rsync examples coming soon.
