Rsync, The Basics On Linux

1st November, 2016 by
Transferring files from one place to another is a pretty basic task. There’s not really much need for more than a basic tool for the task. While tools such as cp (copy) and mv (move) do some simple jobs well, sometimes you need a little bit more. This is where rsync comes along. Now a standard feature of almost all Linux distributions and ported to Mac OS and Windows, rsync takes the idea of transferring files and adds in some very funky features. Let’s look at some rsync examples.

The main design of rsync is for copying files from one system to another over a network. As this can be a very bandwidth intensive task, rsync has a couple of tricks to help things along. First, it analyzes the files in the source and destination directories to ensure that it only transfers files that are different between the two. Second, it can compress the files before transferring to ensure the minimum amount of bandwidth is used. Another handy feature of rsync is to tunnel the file transfers through an SSH connection so that your data is also encrypted during the transfer.

Using rsync is pretty simple:

rsync <options> <source> <destination>

File Or Directory

The source and destination can be a file or a directory. When copying a directory the use of a trailing slash makes a difference to how files are copied. Without a trailing slash on the source directory, the directory itself and its contents are copied to the destination directory. With a trailing slash on the source directory only the contents of the directory are copied to the destination directory. So with a brief example:

rsync /source/dir /dest/dir

rsync /source/dir/ /dest/dir

The two rsync examples work slightly differently. With the first a directory named dir would be created and the files inside copied, so you would find your files in the /dest/dir/dir directory. With the latter, only the files are copied and would be found in the /dest/dir directory.

Now let’s look at some of the rsync examples options:

-r recurse through subdirectories, copying them and their contents.

-l links, copy syminks as symlinks

-p preserve the permissions from the source file

-t preserve modification times

-o preserve the owner

-g preserve the group

-a archive, applies all the above settings in one option, saving a bit of typing.

-E preserve executability

-z compress data during file transfer

--delete delete files from the destination that do not exist in the source

--progress displays the progress of the transfer on screen.

-e Allows the use of an external shell with the transfer, such as ssh

Generally, commonly used options are a, z and progress. The delete option is one to be careful of as if you are using rsync to perform basic automated backups by syncing two directories. If you accidentally delete a file then the deletion will be replicated at your destination.

Copying Files To A Remote System

Copying files to a remote system is one of the core tasks that rsync is used for and to indicate you aim to copy to a remote system you can include the syntax of user@hostname in either source or destination. You can use either the domain name or IP address of the remote system for the hostname, though the user should be the username of the user that has access to read/write files in the remote directory. For example:

rsync -az --progress /home/hayden/projects/

The above command copies the contents from /home/hayden/projects on my local machine to /home/hayden/projects-backups on,. By default, rsync will attempt to connect using SSH on port 22. If your server uses a non-standard port then you’ll need to specify this:

rsync -az --progress -e ‘ssh -p 2222’ /home/hayden/projects/

This command performs the same task but connects to ssh using port 2222. Note that when you copy via SSH you will be prompted for your password or passphrase for your SSH key unless you have configured a key without a passphrase.