Synchronize Files And Directories

rsync is a very handy tool for syncing the files and folders in two directories. rsync only copies files that have changed since the time they were sync’d.

Make Two Directories

mkdir origin destination

Add Files In Origin Directory

touch origin/file.txt

View Origin Directory Contents

ls -l origin
total 0
-rw-rw-r-- 1 chris chris 0 Jul 31 20:04 file.txt

View Destination Directory Contents

ls -l destination
total 0

Sync Origin Directory To Destination Directory

In this code we sync (rsync) all the files and subdirectories (-a) in origin to destination while printing out details of the process (v), deleting (--delete) any files in destination that no longer exist in origin.

rsync -av --delete origin destination
sending incremental file list

sent 136 bytes  received 39 bytes  350.00 bytes/sec
total size is 0  speedup is 0.00

View Destination Directory Contents

Notice that file.txt in origin is now copied over to destination/origin.

ls -l destination/origin
total 0
-rw-rw-r-- 1 chris chris 0 Jul 31 20:04 file.txt