How to synchronize files and directories from one location to another on Unix

Although tar can be used to synchronize/copy files/directories from one location to another (i.e. “tar -cf – -C srcdir . | tar -xpf – -C destdir”), it is really an archive tool and does not do the job as well as rsync. rsync does not have to go through the archive stage and can be used to synchronize one filesystem (or part of it) from one location (local or remote) to another (local or remote). rsync can do full or incremental synchronization. Although rsync is available on Unix, Mac and Windows, just the Unix usage will be described below.

To do a full sync of one location to another:
rsync -Havx srcdir/ destdir

To do an incremental sync after a full sync above:
rsync -Havx srcdir/ destdir (same command)

Note that for incremental sync, data added at the source (after the initial full sync) will be copied to the destination, but data deleted at the source will not be deleted at the destination. To force an incremental sync to delete data in the destination that is no longer in the source (this is like a real sync that is sometimes dangerous), do:
rsync -Havx —delete srcdir/ destdir

If you’re using a filesystem with a snapshot feature, include the option: