How to Transfer Files with Rsync over SSH

January 31, 2020

Introduction

Rsync stands for "Remote Sync." The rsync command lets you transfer and synchronize data between different machines and directories. Using the Secure Shell (SSH) protocol, you can copy your files securely to another location.

The rsync tool has many benefits when compared to 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 via command line/terminal window
  • Rsync installed on the local and destination machine

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-get:

sudo apt-get install rsync

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

checking if rsync is installed on ubuntu

On RPM-based machines, such as CentOS, use:

sudo yum install rsync

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 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 what we recommend. This option syncs recursively and keeps all permission and file settings. This time do not use the asterisk in the source path.

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 plays an important role. If you enter a source directory path without the slash at the end, rsync first transfers the source directory and then its contents. To demonstrate, we will remove the trailing slash from the example above:

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


When we open the test directory, it contains the Dir1 directory and then the rest of the files in Dir1.

Check Rsync File Transfer Progress

To check the status of rsync transfers, use the -P option. This option displays the transfer times, as well as 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 when there haven't been any changes in the source directory, the transfer does not occur. The reason is that rsync only transfers modifications in files 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 there is a change even in one source file, rsync detects it and syncs it with the target.

We made a change in one file and re-ran the same command:

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

The output shows the source sample2 file was transferred since rsync detected that we modified the file.

Conclusion

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

Was this article helpful?
YesNo
Goran Jevtic
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
Next you should read
How to Generate & Set Up SSH Keys on Debian 10
September 14, 2020

This article will help system administrators configure SSH Keys on Debian 10. The instructions allow you to...
Read more
How to Install / Enable OpenSSH on CentOS 7
August 28, 2019

This article provides all the information you need in order to set up SSH encryption on your remote device...
Read more
How to Enable SSH on Debian 9 or 10
March 20, 2024

If you are using Debian 9 or Debian 10 to manage servers, you must ensure that the transfer of data is as...
Read more
How to Enable SSH on Ubuntu 20.04
October 3, 2024

When establishing a remote connection between a client and a server, a primary concern is ensuring a secure...
Read more