rsync Command Tutorial with Examples

The rsync is a utility to transfer and synchronize files and folders between different computers in an efficient and secure way. The rsync command is provided by all Linux distributions, Unix systems. The rsync command provides easy synchronization, compression for network optimization, multithread performance, etc. In this tutorial, we examine how can we use the rsync command in different ways.

rsync Command Syntax

The rsync command has the following syntax.

Local:            rsync [OPTION...] SRC... [DEST]        

Access via remote shell:            
        rsync [OPTION...] [USER@]HOST:SRC... [DEST]            
        rsync [OPTION...] SRC... [USER@]HOST:DEST        

Access via rsync daemon:            
        rsync [OPTION...] [USER@]HOST::SRC... [DEST]                
        rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]            
        rsync [OPTION...] SRC... [USER@]HOST::DEST                
        rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST)

rsync Command Help

The rsync command provides different help options. The most basic one is just running the rsync command without any option or parameter. It lists the most popular options with a basic explanation.

$ rsync
rsync Command Help

Synchronize Directories

The most popular usage for rsync is synchronizing two directories. The source directory is provided as the first parameter and the destination directory is provided as the second parameter. In the following example, we synchronize the source directory data2 into the destination directory data3.

$ srync -r data2/ data3

The -r option is used to synchronize recursively.

Synchronize From Local System To Remove System

The rsync command can be used to copy and synchronize from the local system to the remote system.

$ rsync -r /mnt/backup [email protected]:/mnt/backup

Synchronize From Remote System To Local System

The following command is used to synchronize from a remote system to the local system. The remote system IP address is and we connect with the user ismail . The /mnt/backup path is synchronized from the remote system to the current local system.

$ rsync -r [email protected]:/mnt/backup /mnt/backup 

Enable Compressions

The rsync command can be used to copy and synchronize different types of files and data. The compressions can be very useful text-type data where lots of space and bandwidth can be saved during transmission. The compression can be enabled with -z or --compress options.

$ srync -r -z data2/ data3

We can also specify the compressions level implicitly by using the --compress-level option like below.

$ srync -r -z --compress-level=8 data2/ data3

Show Progress

The rsync command is generally used to transfer multiple files and folders which may take some time. The progress about each file can be displayed on the command line by using the --progress option.

$ srync -r --progress data2/ data3

Don’t Run Just Make Test – Dry Run

Sometimes we may need to only test the synchronization how it can be executed but do not really synchronize. This can prevent errors about delete files and folders. The –dry-run or -n options can be used to make tests about copy. This is called as Dry Run .

$ srync --dry-run -r data2/ data3

Delete Non-Existing Destination Files and Folders

The mirror operation simply copies source files and folders to the destination and removes destination files and folders that do not exist in the source. The --delete option is used to delete non-existing destination files and folders in the destination.

$ srync --delete -r data2/ data3

Exclude Files and Folders

While using rsync we may need to exclude some files and folders. We can use the --exclude option and specify the file or directory we want to exclude. In the following example, we exclude /mnt/backup/binary .

$ srync --exclude=/mnt/backup/binary -r /mnt/backup  /var/backup

Exclude Specific Extensions

While copying files we can specify some file extensions or file types we want to prevent from synchronization. The –exclude= option is used with the extension we want to exclude. In the following example, we exclude the *.txt extension. To prevent errors use double quotes for the extensions.

$ srync --exclude="*.txt" -r /mnt/backup  /var/backup

Use Non-Default SSH Port

By default, the remote synchronization uses the SSH with the default port number 22. But in some cases, we may need to specify the SSH port explicitly which is generally a non-default port number. The -e option can be used to specify the SSH port number. In the following example, we specify the SSH port number as 2222.

$ rsync -e "ssh -p 2222" -r /mnt/backup [email protected]:/mnt/backup

Leave a Comment