Sync two local folders in bash

Solution 1:

There are many more properties to rsync than the previous answer mentions. You should look into them yourself:

man rsync

But for your problem I'd suggest:

rsync -a --delete X Y

This way, the receiver (Y) will delete any file it has that is not in X. Be sure you do it right though. Used incorrectly as root can easily kill your system... :)

Solution 2:

Take a look at rsync(1). It's intended to do pretty much exactly what you want.

rsync -av X Y

It supports remote copying, through its own protocol or ssh, but it can also be used locally.

Depending on the exact behaviour you want you may also want to pass the --update option. This will tell rsync not to overwrite files which already exist on the target and are newer than the ones in the source.

If you want to remove extra files (i.e. files which exist in Y, but not in X) you can add the --delete option.

Solution 3:

You might use Fitus/Zaloha.sh. It first calculates the necessary synchronization actions, presents them to you, asks for confirmation and then executes them:

$ Zaloha.sh --sourceDir="X" --backupDir="Y"