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.
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 Distribution | Command to Install rsync |
---|---|
Debian/Ubuntu | sudo apt install rsync |
Fedora/RHEL | sudo dnf install rsync |
Arch Linux | sudo pacman -S rsync |
openSUSE | sudo 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 Option | Description |
---|---|
-r | Recursively syncs directories and their content but does not keep file ownership, permissions, timestamps, or symbolic links. |
-a | The archive mode behaves like the recursive mode but keeps all file permissions, symbolic links, file ownership, etc. |
-z | Used to compress data during transfers to conserve bandwidth. |
-b | Makes backups of files that are going to be overwritten or deleted during synchronization. |
-h | Outputs numbers in a human-readable format. |
-n | Performs a dry run. Used for testing before the actual synchronization takes place. |
-e | Makes backups of files that will be overwritten or deleted during synchronization. |
-- progress | Displays the transfer progress during synchronization. |
-v | Verbose output. Displays the details of the transfer. |
-q | Quiet 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.
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
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
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
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
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
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
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
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
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/
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/
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
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/
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
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)
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.
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.