How to Transfer Files with Rsync over SSH

By
Goran Jevtic
Published:
January 31, 2020

The rsync command lets you transfer and synchronize data between different machines and directories. Using the Secure Shell (SSH) protocol, users can copy files securely to another location.

The rsync tool offers many benefits over other methods for copying files. It uses both compression and decompression while sending and receiving files. The tool only transfers new or updated files saving on bandwidth and bringing faster transfer times.

Follow this guide to learn how to use rsync over SSH to transfer or copy files.

tutorial on rsync transfer or copy files ssh

Prerequisites

  • User with sudo or root privileges.
  • SSH access to a server.
  • Rsync installed on the local and destination machines.

Verify Rsync Installation

Most recent Linux distributions have rsync by default. To verify your system has rsync installed, run the installation command.

For Debian and Ubuntu machines, use apt:

sudo apt install rsync

On RPM-based machines, such as Rocky Linux, use:

sudo yum install rsync

If rsync is already installed, the output shows the current version of the tool.

Transfer Files with Rsync over SSH

Before you can start transferring files and directories with rsync over SSH, make sure you can use SSH to connect to a remote server. Once verified, you can begin backing up your data. Ensure your destination system has sufficient storage space.

The syntax for copying files to a remote server over SSH with the rsync command is:

rsync OPTION SourceDirectory_or_filePath user@serverIP_or_name:Target

Note: You need to enter your password every time you run the rsync command. To avoid doing so, you can set SSH key-based authentication. You can omit the username from the command if you want to use the currently logged in user.

rsync Command Examples

Follow the rest of the guide to see the examples of the most common rsync use cases.

Transfer a Specific File with Rsync

To transfer a single file to a remote machine, enter the full path of the source file. For example:

rsync ~/Dir1/source.pdf [email protected]:~/Desktop/test

Make sure you use the quotes for files that contain spaces in the name.  For example:

rsync ~/Desktop/Dir1/"source pdf sample.pdf" [email protected]:~/Desktop/test

Transfer Contents of a Directory with Rsync

There are a few options when syncing directory contents with rsync over SSH.
To transfer all files from a source directory to a target on a remote host, enter:

rsync ~/SourceDirectory/* [email protected]:~/Destination

The asterisk (*) instructs the tool to include all files in the source directory. Subdirectories are not transferred.

For example:

rsync excluding subdirectories during a transfer

The output shows the directories that rsync skipped during the transfer.

To include all subdirectories from the source directory, use the -r (recursive) or -a (archive) option. The -a flag is recommended. This option syncs recursively and keeps all permissions and file settings:

rsync -a ~/Desktop/Dir1/ [email protected]:~/Desktop/test

When you run the command in this format, rsync transfers all files and subdirectories from the source directory to the target location. From our example above, the test folder will have the contents of the Dir1 source when the transfer completes.

To omit files from being copied check out our guide on how to exclude files and directories in data transfer using rsync command.

Note: The trailing slash in the source path is important. If a source directory path is entered without the slash at the end, rsync first transfers the source directory and then its contents.

Check Rsync File Transfer Progress

To check the status of rsync transfers, use the -P option. This option displays the transfer times and the names of the files and directories that are synced.

If there is an issue with your connection and the sync is interrupted, -P resumes your transfers.

Run the command in this format to sync recursively and check the status of the transfer:

rsync -aP ~/SourceDirectory/ [email protected]:~/Destination

For example:

The rsync command output when checking the status of the transfer using the -aP option.

If you rerun the same command after there have been no changes in the source directory, the transfer does not occur. The reason is that rsync only transfers file modifications and new files. In this case, the output shows only the following line:

Running the rsync command with the status option when there are no modifications or new files

When a source file changes, rsync detects the change and syncs it with the target.

output for the rsync command with the status option when there is a modification or a new file to transfer

The output shows that the source sample2 file was transferred because rsync detected that it had been modified.

Conclusion

With the Linux commands in this guide, you should be able to transfer files with rsync over SSH. We listed the most common rsync options so you can successfully back up your data to a remote location.

Was this article helpful?
YesNo