How to synchronize data using rsync tool

My favourite tool, which I use to synchronize data between two sources, is ‘rsync‘. Here I’m going to show you how I use it to mirror data from one local folder to another.

I have two folders. The first one is located on a USB flash drive where I keep my archive of documents, let’s call it ‘source folder’. Other folder is located on a local machine, I will reference it as a ‘target folder’.

Now I’m going to mirror all files from the source folder to the target folder. In a terminal window I run the following command:

The command above will compare each directory within the source and target folders recursively and list all files which rsync is going to replace or delete within the target folder. Please note, that I put the ending “/” symbol at the end of the source folder path, otherwise rsync would copy the source directory itself into the target folder. If I’m fine with the list I just get rid of -n option(which means dry run) and launch the command again:

Please be aware, that when you use –size-only flag, rsync will not compare files that match in size, this is a rough, but fast type of comparison. If you want to compare files by their checksums, then remove –size-only flag and add ‘-c’ key when launching rsync. This type of comparison consumes much more time, but it’s very precise:

As in previous example, just remove the dry run option -n when you’re ready to synchronize your folders. Make backup copies of your files until you get get acquainted with rsync.

Check rsync manual page, there are tons of different options.