rsync Command in Linux: Syntax, Options, Examples

September 19, 2024

Introduction

Rsync is a free command-line tool for transferring files within your local system and between local and remote systems. It offers many customization options and is often used for mirroring, performing backups, or migrating data to other servers.

Rsync only copies changes from the source, making transfers fast and efficient.

Learn how to use rsync with 20 command examples that cover the most common use cases in Linux.

Basic rsync commands in Linux.

Prerequisites

  • User with sudo or root privileges.
  • Access to a terminal/command line.
  • SSH access for remote rsync examples.

Install rsync (Optional)

Rsync is preinstalled on most modern Linux distributions. Use the command for your distribution to verify the installation or install rsync if necessary:

Linux DistributionCommand to Install rsync
Debian/Ubuntusudo apt install rsync
Fedora/RHELsudo dnf install rsync
Arch Linuxsudo pacman -S rsync
openSUSEsudo zipper install rsync

rsync Command Syntax

The simplest form of the rsync command is used for copying files or directories from one location to another on the same system:

rsync [OPTION] [SOURCE] [DESTINATION]

The source and destination are a directory or file path.

When performing remote data transfers, you must specify the address of the remote host. Use the following syntax to synchronize local files and directories to a remote server:

rsync [OPTION] [SOURCE] [USERNAME]@[HOSTNAME_OR_IP]:[DESTINATION]

You can also synchronize files and directories from a remote system to your local machine:

rsync [OPTION] [USERNAME]@[HOSTNAME_OR_IP]:[SOURCE] [DESTINATION]

Note: When transferring files to a remote system, use the -e option with the SSH command to encrypt and secure the transfer channel.

rsync Options

The following table includes some of the most common rsync options:

rsync OptionDescription
-rRecursively syncs directories and their content but does not keep file ownership, permissions, timestamps, or symbolic links.
-aThe archive mode behaves like the recursive mode but keeps all file permissions, symbolic links, file ownership, etc.
-zUsed to compress data during transfers to conserve bandwidth.
-bMakes backups of files that are going to be overwritten or deleted during synchronization.
-hOutputs numbers in a human-readable format.
-nPerforms a dry run. Used for testing before the actual synchronization takes place.
-eMakes backups of files that will be overwritten or deleted during synchronization.
-- progressDisplays the transfer progress during synchronization.
-vVerbose output. Displays the details of the transfer.
-qQuiet mode. Used to suppress the output for the rsync command and options.

rsync Examples

Rsync is a versatile synchronization tool that can be customized for specific use cases. The following examples cover the most common scenarios.

Note: Pay special attention to the trailing slash (/) in the source path when syncing directories. If you include the trailing slash in the source path, rsync copies the content of the source directory to the destination but does not create the source directory itself. If you omit the slash, rsync will also create the source directory inside the destination directory.

1. Copy Single File Locally

To copy one file to another directory on a local machine, enter the source file's path, followed by the target destination. For example:

rsync -v /home/pnap/Desktop/sample.txt /home/pnap/Desktop/rsync/

This command transfers the sample.txt file to the rsync directory. If the destination directory does not exist, add a slash at the end, and rsync will create it, as shown in the example.

Copying a file on alocal machine using the rsync command.

The -v option is used to display the transfer details.

Note: When copying a file from the current working directory, enter the file name without specifying the full path.

2. Copy Multiple Files Locally

To copy multiple files with a single rsync command, list the full paths of the source files followed by the destination directory:

rsync -v /home/pnap/Desktop/sample.txt /home/pnap/Desktop/sample2.txt /home/pnap/Desktop/rsync
Using rsync to copy multiple files locally.

The command transfers the sample.txt and sample2.txt files to the rsync directory. Use this method when copying a smaller number of files. If you want to transfer a larger list of files, use the --exclude option instead.

3. Recursively Copy a Directory and All Subdirectories

To copy a directory and its contents to another location on your machine, use the -a (archive) or -r (recursive) option. This example shows how to use the archive option:

rsync -av /home/pnap/Desktop/test_project /home/pnap/Desktop/rsync
Copy directory recursively using rsync.

The rsync tool copies the test_project directory to the rsync directory. Note that we did not use a trailing slash after test_project. As a result, rsync created the test_project directory and placed it and its content inside the rsync directory.

4. Transfer a File or Directory from Local to Remote Machine

To copy the test_project directory to the remote_project directory on a remote machine, specify the destination IP address or hostname. Add the IP address and the full destination path after the source directory, separated by a colon (:). For example:

rsync -av /home/pnap/Desktop/test_project 10.0.2.15:/home/test/Desktop/remote_project

If you want to use another account for the remote connection, enter the username before the IP address:

rsync -av /home/pnap/Desktop/test_project test@10.0.2.15:/home/test/Desktop/remote_project
Using rsync to transfer a directory to a remote server.

To transfer a single file to a remote host, specify the file's path followed by the destination path:

rsync -av /home/pnap/Desktop/sample1.txt test@10.0.2.15:/home/test/Desktop/remote_project

After entering the command, you are prompted to enter the password for the remote user account to proceed with the transfer.

5. Transfer Multiple Files or Directories from Local to Remote Machine

To copy multiple files or directories to a remote machine, list their paths followed by the destination directory:

rsync -av /home/pnap/Desktop/test_project /home/pnap/Documents/local_dir1 test@10.0.2.15:/home/test/Desktop/remote_project
rsync multiple directories to a remote machine.

In this example, the test_project directory and the local_dir1 directory are transferred to the remote_project directory on the remote machine at 10.0.2.15. The -a option ensures the transfer is recursive and preserves file attributes, while the -v option displays transfer details.

6. Use SSH for Secure Remote Transfers

The -e option specifies the shell program for rsync file transfers. Append -e ssh to the rsync command to transfer files securely over SSH:

rsync -ve ssh /home/pnap/Desktop/sample2.txt test@10.0.2.15:/home/test/Desktop
Using rsync with SSH to sync files.

In this example, the sample2.txt file is transferred from your local desktop to the remote desktop via SSH. The -v flag displays details about the transfer process.

7. Retrieve File or Directory from Remote to Local Machine

Rsync can also transfer files from a remote server to your local machine. To pull a directory from a remote server, specify it's IP address:

rsync -av test@10.0.2.15:/home/test/Desktop/remote_project /home/pnap/Downloads
Retrieve directory from remote server using rsync.

The remote_project directory from the remote server at IP 10.0.2.15 was copied to the /home/pnap/Downloads directory on the local server.

To copy a specific file from a remote host, enter the full path of the source file and the destination on your local machine. For example:

rsync -av test@10.0.2.15:/home/test/Desktop/remote_project/sample7.txt /home/pnap/Downloads

The sample7.txt file from the remote server is copied to the Downloads directory on the local machine.

8. Retrieve Multiple Files or Directories from Local to Remote Machine

To transfer multiple files or directories from a remote server, list the paths using curly brackets after the remote server's IP address. Separate the paths with a comma. For example:

rsync -av test@10.0.2.15:{/home/test/Desktop/remote_project,/home/test/Desktop/rsync} /home/pnap/local_project
Copy multiple directories or files from a remote server using the rsync command.

The command can be applied to individual files. List as many files as you need inside the curly brackets.

9. Display Transfer Progress

When performing a large data backup, you can track the transfer's progress. Add the --progress flag to view the amount of data transferred, transfer speed, and remaining time.

For example, to back up backup1.zip to a remote server and show the progress, enter:

rsync -av --progress /home/pnap/Desktop/backup1.zip test@10.0.2.15:/home/test/Desktop/rsync
Show the rsync transfer progress.

The output shows the file size, progress, transfer speed, and ETA.

10. Do a Dry Run Before Syncing

Users can perform a dry run to test if rsync will behave as expected before syncing files. To do a dry run, add the --dry-run option to your rsync command:

rsync -av --dry-run --delete /home/pnap/Desktop/remote_project/ test@10.0.2.15:/home/test/Desktop/remote_project/
Performing an rsync dry run.

The output looks the same as syncing files, except no data will be synced or deleted. To confirm the operation was a test and not the actual transfer, the terminal displays (DRY RUN) at the bottom.

11. Remove Non-existent Source File or Directory from Destination

You can use the --delete option to remove any file or directory at the destination that does not exist in the source:

rsync -av --delete /home/pnap/Desktop/remote_project/ test@10.0.2.15:/home/test/Desktop/remote_project/
Delete files in remote directory to sync with local directory.

In this example, the content of the remote_project directory on your local machine is synchronized with the remote_project directory on the remote machine. Files or directories in the remote destination that are not present in the local source directory are deleted to ensure the destination is an exact mirror of the source.

Note: The --delete option permanently removes files from the destination. Use it with caution.

12. Delete Source Files After Transfer

Users sometimes need to delete source files after the transfer. For example, when moving a weekly backup to a new server, you may no longer need the source file on the old server.

Use the --remove-source-files flag to transfer and delete the specified source file:

rsync -v --remove-source-files /home/pnap/Desktop/backup1.zip test@10.0.2.15:/home/test/Desktop

The command transfers the backup file backup1.zip to the Desktop directory on the remote machine. Once the transfer is complete, the source file on the local machine is deleted.

13. Set Maximum File Size for Transfer

The --max-size=add_size flag sets the maximum size of the file to be transferred. This option allows users to avoid large file transfers and conserve resources. For example, the following command instructs rsync to only transfer files that are smaller than 500KB:

rsync -av --max-size=500k /home/pnap/Desktop/test_project test@10.0.2.15:/home/test/Desktop/rsync

Files in the test_project directory that exceed the defined size are not transferred to the remote server.

14. Set Minimum File Size for Transfer

Use --min-size=add_size with rsync to skip the transfer of files smaller than a specified size. This option is useful, for example, when you want to skip small log or thumbnail files. For example:

rsync -av --min-size=10k /home/pnap/Desktop/test_project test@10.0.2.15:/home/test/Desktop/rsync

This command ensures that only files larger than 10KB are transferred to the remote server.

15. Limit Bandwidth Usage During Transfer

To limit bandwidth usage during data transfers, use the --bwlimit=KB/s flag. To set the maximum transfer speed to 50KB/s, enter:

rsync -av --bwlimit=50 --progress /home/pnap/Desktop/test_project test@10.0.2.15:/home/test/Desktop/rsync
Limiting bandwidth for rsync file transfer.

This example also uses the --progress option to illustrate how --bwlimit controls the transfer speed.

16. Transfer Specific File Types

You can use rsync to copy only a specific file type. To do so, use the asterisk (*) symbol instead of the file name and add the extension.

For example, to copy only .txt files, enter:

rsync -v /home/pnap/Documents/*.txt /home/pnap/Desktop/rsync/
Use rsync filters to copy specific file types.

The command transfers all text files from the Documents directory to the rsync directory on your desktop.

17. Copy Directory Structure but Skip Files

Rsync allows users to mirror the directory structure without transferring the actual files. To do so, add -f"+ */" -f"- *" filters before the source directory:

rsync -av -f"+ */" -f"- *"  /home/pnap/Desktop/test_project /home/pnap/Documents
Only copy a directory structure using rsync.

The test_project directory structure was copied to the Documents directory, while no files were transferred.

18. Add Date Stamp to Directory Name

You can add a date stamp to a directory name during the transfer. This option helps you track when transfers took place without opening directory properties. To do this, append $(date +%Y-%m-%d) to the destination directory name:

rsync -av /home/pnap/Desktop/test_project /home/pnap/Desktop/rsync$(date +%Y-%m-%d)
Add date stamp to directory name via rsync.

In this example, the destination directory name includes the current date in the specified format (YYYY-MM-DD).

19. Do Not Copy Source File If Destination File is Modified

Sometimes, you may modify a file at the destination and want to prevent rsync from overwriting it with a file from the source. To avoid overwriting modified destination files, use the -u (update) option:

rsync -avu /home/pnap/Desktop/test_project/sample4.txt /home/pnap/Desktop/rsync/

In this example, if the sample4.txt file in the rsync directory is modified, the rsync tool will not overwrite it with the version from the test_project directory.

20. Show Difference Between Source and Destination Directory

You can use the -i flag with the rsync command to check for differences between the source and the destination:

rsync -avi /home/pnap/Desktop/test_project/ /home/pnap/Desktop/rsync

In this example, the output indicates that the sample11.txt file is missing at the destination.

Checking the difference between source and destination with rsync.

Other possible letters in the output are:

  • f - Stands for file.
  • d - Indicates a directory.
  • t - Shows the timestamp has changed.
  • s - Shows the size has changed.

Conclusion

This guide provided 20 rsync command examples for transferring files and directories locally and remotely.

Next, learn to use the sync command to synchronize cache data to permanent system memory, save important files, and prevent data loss.

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 Secure SSH Connections: 5 Best Practices
September 24, 2019

The article covers the 5 most common and efficient ways to secure an SSH connection. The listed solutions go a long way in preventing malicious activity and protecting your servers.
Read more
Backup Strategy: Ultimate Guide for Data Backup
September 19, 2024

This article provides a step-by-step guide to creating well-rounded and cost-effective data backup strategies. learn how to ensure critical files are restorable no matter what goes wrong with the original data set.
Read more
How to Set Up Passwordless SSH Login
October 3, 2024

Speed up connecting to remote servers by enabling passwordless SSH login via public key authentication. In this guide you will learn how to set up and enable passwordless login.
Read more
SSHFS: Mount Remote File Systems on Linux & Windows
August 20, 2024

Need to transfer files between two machines securely? Try the SSHFS client, which mounts a remote file system directly to your local machine and uses the SSH file transfer protocol.
Read more