I am Hack Sparrow
Captain of the Internets.

RSYNC Tutorial with Examples

Need a fast and efficient way to synchronize files on two systems? Looking for an alternative to FTP / SCP / RCP? RSYNC is the right tool for it!

RSYNC's syncing capability makes it an excellent alternative to FTP and other file transfer tools like SCP and RCP. Why would one want to use RSYNC for file transfer? It is efficient (does not transfer files which are already in sync), safe (uses SSH), and offers a lot of useful capabilities that FTP and other tools don't offer easily. Maybe FTP is disabled on the server because of security concerns?

In this tutorial, I will show you some of the most common use cases of RSYNC:

Upload files

Upload all the contents of the current directory to `/home/captain/websites/ahoy` on Enter the password for the user when prompted for it.

$ rsync -zvr ./ captain@

Upload files and show the progress

$ rsync -zvr --progress ./ captain@

Delete files that don't exist in the local copy

$ rsync -zvr --delete ./ captain@

Upload files but exclude some files and directory

We want to exclude all files starting with a dot `.`, and a directory named `node_modules`.

$ rsync -zvr --exclude=".*/" --exclude node_modules/ ./ captain@

RSYNC options can be used together

$ rsync -zvr --delete --progress --exclude=".*/" --exclude node_modules/ ./ captain@

Use a public key (.pem file) with RSYNC

We will used a public key named `server-key.pem` instead of supplying a password to authenticate. The key is in the current directory in this case.

$ rsync -zvr -e "ssh -i server-key.pem" ./ captain@

Download files

We will download the contents of the directory `/home/captain/websites/ahoy` on to the current directory. The directory `ahoy` will be created in the current directory with all its contents.

$ rsync -zvr --progress --exclude=".*/" captain@ ./

Wondering what the options zvr means?

z - compress data (faster transfer)
v - verbose
r - recurse into directories

That should be enough for most basic RSYNC requirements. If you want to know more, open a terminal and type man rsync.

Note: if you are using SCP or RCP, it is high time you switched to RSYNC - because RSYNC was written specifically to replace them.

2 Responses to “RSYNC Tutorial with Examples”

  1. Brent J. Nordquist says:

    Thanks for this page! The PEM example was just what I needed.

  2. Toni Rocha says:

    Hello, how parameter must I use to download only unmodified files? Thanks!

Make a Comment