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.
- 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
sudo apt-get install rsync
If rsync is already installed, the output shows the current version of the tool.
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 [email protected]_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.
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
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:
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:
The output shows the source sample2 file was transferred since rsync detected that we modified the file.
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.