rsync: Tutorial with examples
Need a fast and efficient way to synchronize files on two systems? Looking for an alternative to FTP /
rsync is the right tool for it!
rsync's syncing capability makes it an excellent alternative to FTP and other file transfer tools like
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
Upload all the contents of the current directory to
/home/captain/websites/ahoy on 192.168.13.37. Enter the password for the user when prompted for it.
$ rsync -zvr ./ email@example.com:/home/captain/websites/ahoy
Upload files and show the progress
$ rsync -zvr --progress ./ firstname.lastname@example.org:/home/captain/websites/ahoy
Delete files that don't exist in the local copy
$ rsync -zvr --delete ./ email@example.com:/home/captain/websites/ahoy
Upload files but exclude some files and directory
We want to exclude all files starting with a dot
., and a directory named
$ rsync -zvr --exclude=".*/" --exclude node_modules/ ./ firstname.lastname@example.org:/home/captain/websites/ahoy
rsync options can be used together
$ rsync -zvr --delete --progress --exclude=".*/" --exclude node_modules/ ./ email@example.com:/home/captain/websites/ahoy
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" ./ firstname.lastname@example.org:/home/captain/websites/ahoy
We will download the contents of the directory
/home/captain/websites/ahoy on 192.168.13.37 to the current directory. The directory
ahoy will be created in the current directory with all its contents.
$ rsync -zvr --progress --exclude=".*/" email@example.com:/home/captain/websites/ahoy ./
Wondering what the options zvr means?
z- compress data (faster transfer)
r- recurse into directories
That should be enough for most basic
rsync requirements. If you want to know more, open a terminal and type
Note: if you are using
rcp, it is high time you switched to
rsync - because
rsync was written specifically to replace them.