Rsync command in Linux with Examples

Updated 29 October 2024

As a system administrator or Linux power user, manually synchronizing directories or sharing files between computer systems can be extremely time-consuming. Sometimes if you lost connection while transferring files, you may require a tool that will resume the transfer process exactly where it left off once the connection is restored. Fortunately, there is an effective command line utility that will assist you in accomplishing this, which we will discuss in this guide.

Rsync Command

Rsync command is a powerful and flexible file copying utility. It can copy locally, to and from another host via any remote shell program, or to and from a remote rsync daemon.

It is well-known for its delta-transfer method using delta transfer algorithm, which lowers the amount of data transferred over the network by transferring only the differences  between the source and the already-existing files at the destination. Rsync command is a popular backup and mirroring tool, as well as an improved copy command for regular use.

In this guide, I'll provide step-by-step instructions on the usage of this tool and in-depth descriptions of the most popular rsync options.

Rsync syntax

Before we begin using the rsync command, let's go over the basic syntax.

The syntax for the rsync command in Linux is as follows:

Here is a breakdown of the Rsync syntax:

  • OPTION - options mentioned above.
  • SRC - directory from which the file is copied.
  • DEST - directory to which the file will be copied.
  • USER - Remote username.
  • HOST - remote machine IP Address or hostname.

Rsync command Options

rsync provides a plethora of options for controlling every aspect of its behavior and allowing for the very broad specification of the list of files to be copied.

Some of the important rsync options are:

OptionDescription
-a, --archiveArchive mode works similarly to the recursive mode (-r) , but it maintains all file permissions, file ownership, symbolic links and so on.
-z, --compressUsed to  compress file data during the transfer.
-h, --human-readableOutput numbers in a human-readable format
-b, --backupMake backups during file transfer
-n, --dry-runInstruct rsync to perform a trial run that does not make any changes. It is commonly used when deleting source files or in conjunction with the —verbose (-v) option to see what the rsync command will accomplish before running it.
-r, --recursiveInstructs rsync to copy a directory and its subdirectories recursively.
--progressShow progress during transfer. This displays the amount of data transferred, the transfer speed, and the time left for the transfer to complete.
-e, --rshThis option lets you select a different remote shell program to use for synchronization between the local and remote copies of rsync. rsync is typically configured to use ssh by default, however on a local network, you may choose to use rsh as a remote shell.
-v, --verboseThis option increases the amount of data logged by the rsync daemon during its initial phase.
-q, --quietThis option reduces the amount of data you receive during the transfer by suppressing information messages from the remote server. This option is useful when using cron to execute rsync.
--backup-dirWhen combined with the —backup option, this instructs rsync to store all backups on the receiving side in the specified directory. This is useful for incremental backups.
-u,--updatePrevent overwriting destination files that have been modified.
--max-sizeinstructs rsync not to transfer any files that are larger than the specified size
--min-sizeInstructs rsync not to transfer any files that are smaller than the specified size.
--remove-source-filesUsed to remove files  from the source directory.
--deleteRemove unnecessary extra files files from the receiving side (files that don't exit from the source)
--portInstead of the default of 873, this specifies an alternate TCP port number for the daemon to listen on.
-i, --itemize-changesCheck the difference between source files and destination files.
--bwlimitLimit the bandwidth used by rsync for efficient network utilization

How to Use Rsync

To use rsync, you must specify a source and a destination. The source or destination can be a remote system.

The most basic application of rsync is to copy a file from one directory to another on the local machine. Here's an illustration of transferring a backup file to another destination directory:

$ rsync -a ~/backup.tar.gz ~/backups

Image from notes

Rsync command examples

1. Copy Data to a local machine

To transfer a file or sync files to another directory within the local machine, enter the complete path of the file, or the file name if it is in your current directory, followed by the desired destination. Here is an example:

$ rsync -av /home/linux/backup.sh /home/linux/projects/

Image from notes

To copy multiple files and directories at once, separate the filenames with spaces and then specify the destination.

$ rsync -av /home/linux/backup.sh logs.txt cat.png  /home/linux/projects/

Image from notes

To recursively copy files or a directory and its subdirectories to the remote machine, use the -a (archive mode) or -r option.

$ rsync -rv logs  newlogs

Image from notes

This will copy all the files to the destination directory (newlogs/)

2. Copy files from the local machine to a remote machine

To copy files and directories to a remote machine, specify the username, IP address of the remote machi, and the location where the files should be copied.

The following command copies the file "log.txt" to the home directory on the specified remote host.

rsync -v logs.txt  linux@REMOTEIP:~/

Image from notes

Before the file transfers can begin, you must enter the remote machine's user password. It will appear as follows:

Image from notes

It is worth noting that, when the filename is not included in the destination path, the file is copied under its original name. You can provide a new filename if you want to save the file under a different name.

3. Copy files from remote to a local host

The Rsync command allows you to transfer files from a remote computer to a local computer.

If you want to copy files from a remote machine to a local host, you can simply achieve that by using the remote location as a source and the local host as the destination.

In this example, we'll transfer the file logs.txt from a remote server (IP) to our local host with the username Linux:

Image from notes

4. Include Specific Data

You can use metacharacter wildcards with rsync to transfer only specific files.  Here is an example using the asterisk (*) to copy only bash script files:

$ rsync -v *.sh  linux@REMOTE-IP:~/

Image from notes

This will copy all the files with the .sh file extension.

5 Exclude files or directories

You can exclude files or directories from the transfer by using the -x option. For example, if you want to exclude /home/linux/backup and its subfolders, you can use this command:

rsync -axv /home/linux/backups linux@REMOTE-IP:~/

Image from notes

6. Limit file transfer size

The —max-size and —min-size options are used to limit the file size that rsync will transfer. The —max-size option instructs rsync not to transfer any files that are larger than the specified size, and the —min-size option instructs rsync not to transfer any files that are smaller than the specified size.

For example, copying files of less than 30 KB:

$ rsync -rv --max-size=3k ~/  [email protected]:~/

Image from notes

This rsync command transfers all that are smaller than 30 Kilobytes.

7. Remove files from the source directory after successful transfer

After the transfer, it is common practice to delete files from the source. For example, you are transferring your files to a new system. Once the transfer is complete, you may no longer need the old files on the old system.

The rsync  --remove-source-files option can be used to remove files  from the source directory. Here is an example of achieving that:

$ rsync --remove-source-files -rv logs  [email protected]:~/

Image from notes

8. Find the difference in Data between SRC and DEST

To check the difference between local files and destination files with rsync, use the -i options. For example:

Image from notes

The example above shows that the logs.txt file on the localhost is larger than the one on the remote machine.

The following letters could appear in the output:f

f - indicates file

d - indicates that the destination file is in question.

t - indicates that the timestamp has changed.

s - indicates that the size has changed.

9. Limit the transfer bandwidth

To limit the bandwidth used by rsync for efficient network utilization, use the –bwlimit option with a number of bytes per second. For example:

$ rsync -rv --bwlimit=3 ~/  linux@REMOTE-IP:~/

Image from notes

10. Avoid overwriting modified destination files

If two directories are kept in sync, rsync won't copy a file if it already exists at the destination. Occasionally you may make changes to the destination file and you want to prevent rsync from overwriting it.

Use the -u option to prevent overwriting destination files that have been modified. For example:

$ rsync -avu logs.txt  linux@REMOTE-IP:~/

Image from notes

11. Mirror of source

To remove unnecessary extra files or extraneous files from the receiving side (files that don't exit from the source), use the –delete option with rsync. This is essential when it comes to keeping the source and destination files in sync. Here is an example to delete files that don’t exist from the source directory.

$ rsync --delete -av logs linux@REMOTE-IP:~/

Image from notes

12. Displaying progress while transferring data

When transferring a large amount of data, you may want to monitor the transfer's progress. Use the —progress option to accomplish this. This option displays the amount of data transferred, the transfer speed, and the time left for the transfer to complete.

Here is an example:

$ rsync -a --progress ubuntu-22.04.1-desktop-amd642.iso linux@REMOTE-IP:~/backups

Image from notes

13. Perform a Dry Run

Use the —dry-run or -n option to instruct rsync to perform a trial run that does not make any changes. It is commonly used when deleting source files or in conjunction with the —verbose (-v) option to see what the rsync command will accomplish before running it. Here is an example:

$ rsync --delete --dry-run -av logs linux@REMOTE-IP:~/

Image from notes

14. Rsync over ssh with a specific Port

Instead of the default of 873, the –port option specifies an alternate TCP port number for rsync the daemon to listen on:

rsync –port 779 -v logs.txt  linux@REMOTE-IP:~/

Image from notes

Conclusion

In this article, we saw how to use the rsync command line utility to efficiently synchronize files between two remote or local hosts in an efficient way through some practical examples. So now that you are familiar with the basic syntax of this command and its options, it is time to start using rsync to sync files between your local host and remote server.

Spotted a mistake or want something added? Send me a note.