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 192.168.13.37. Enter the password for the user when prompted for it.
$ rsync -zvr ./ captain@192.168.13.37:/home/captain/websites/ahoy
Upload files and show the progress
$ rsync -zvr --progress ./ captain@192.168.13.37:/home/captain/websites/ahoy
Delete files that don't exist in the local copy
$ rsync -zvr --delete ./ captain@192.168.13.37:/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 node_modules
.
$ rsync -zvr --exclude=".*/" --exclude node_modules/ ./ captain@192.168.13.37:/home/captain/websites/ahoy
rsync options can be used together
$ rsync -zvr --delete --progress --exclude=".*/" --exclude node_modules/ ./ captain@192.168.13.37:/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" ./ captain@192.168.13.37:/home/captain/websites/ahoy
Download files
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=".*/" captain@192.168.13.37:/home/captain/websites/ahoy ./
Wondering what the options zvr means?
z
- compress data (faster transfer)v
- verboser
- 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.