Did You Know?

How Can We Help?

Special Offer

Deploy your Cloud Server now and get $100 in Free Credits!

Get Started

How To Use Rsync to Sync Local and Remote Directories

Table of Content
How To Use Rsync to Sync Local and Remote Directories

This article will teach you How to Use Rsync to Synchronize Local and Remote Directories. 


Rsync, which stands for "remote sync," is a tool for keeping files in sync both remotely and locally. It uses an algorithm to move only the parts of files that have changed, which reduces the amount of data that needs to be copied.

In this tutorial, we'll explain what Rsync is, go over the syntax for using Rsync, show you how to use Rsync to sync with a remote system, and talk about other options you have.


To practise using rsync to sync files between a local system and a remote system, you'll need two computers, one to act as your local system and the other as your remote system. As long as they are set up the right way, these two machines could be virtual private servers, virtual machines, containers, or even personal computers.

If you want to use servers to follow this guide, it would be smart to give each of them administrative users and set up a firewall. Follow our First Server Setup Guide to set up these servers.

To follow this tutorial, you will need to have made SSH keys on both of the machines you use. This is true no matter what kind of machines you use. Then, follow Step 2 of that guide to copy each server's public key to the authorized keys file of the other server.

This guide was tested on computers running Ubuntu 20.04, but it should work on most Linux-based computers with rsync installed if rsync is installed.

To Use Rsync to Sync Local and Remote Directories please read the below steps ....

Defination Rsync

Rsync is a very flexible tool for synchronising files over a network. It comes with most Linux distributions by default because it is common on Linux and Unix-like systems and is a popular tool for system scripts.

Recognizing Rsync Syntax

rsync's syntax is similar to that of other tools like ssh, scp, and cp.

First, run the following command to go to your home directory:

[console]#cd [/console]

after that create a test directory

[console]#mkdir dir1[/console]

Make a second test directoey:

[console]#mkdir dir2[/console]

Add some test files now:

[console]#touch dir1/file{1..100}[/console]

There is currently a dir1 directory with 100 empty files in it. Verify by listing the files out:

[console]#ls dir1[/console]

file1 file18 file27 file36 file45 file54 file63 file72 file81 file90
file10 file19 file28 file37 file46 file55 file64 file73 file82 file91
file100 file2 file29 file38 file47 file56 file65 file74 file83 file92
file11 file20 file3 file39 file48 file57 file66 file75 file84 file93
file12 file21 file30 file4 file49 file58 file67 file76 file85 file94
file13 file22 file31 file40 file5 file59 file68 file77 file86 file95
file14 file23 file32 file41 file50 file6 file69 file78 file87 file96
file15 file24 file33 file42 file51 file60 file7 file79 file88 file97
file16 file25 file34 file43 file52 file61 file70 file8 file89 file98
file17 file26 file35 file44 file53 file62 file71 file80 file9 file99[/console]

dir2 is also empty. To sync dir1 to dir2 on the same system, execute rsync with the "recursive" -r flag:

[console]#rsync -r dir1/ dir2[/console]

-a is a combo flag that means "archive." It syncs recursively and preserves symbolic links, special and device files, modification times, groups, owners, and rights. This flag is preferred over -r. Run the same command with the -a flag:

[console]#rsync -a dir1/ dir2[/console]

Please note that the previous two instructions have a trailing slash (/) after the first argument.

[console]#rsync -a dir1/ dir2[/console]

Trailing slash indicates dir1's contents. Without the trailing slash, dir1 would be inside dir2. The result is a hierarchy:

[console]Trailing slash indicates dir1's contents. Without the trailing slash, dir1 would be inside dir2. The result is a hierarchy:[/console]

Check your rsync command arguments before executing. Using Rsync's -n or —dry-run option does this. The -v flag, which means “verbose”, is also necessary to get the appropriate output. You’ll combine the a, n, and v flags in the following command:

[console]#rsync -anv dir1/ dir2[/console]

sending incremental file list

Now compare that output to what you get when you remove the trailing slash, like in:

[console]#rsync -anv dir1 dir2[/console]

sending incremental file list

This output shows that the directory was migrated, not just its files.

Keeping a Local System in Synch with a Remote One Using Rsync

To utilise rsync with a remote system, you need SSH access and rsync on both machines. Once SSH access is confirmed between two machines, use the following syntax to sync dir1 to a remote machine. In this example, you'll omit the trailing slash to transmit the real directory:

[console]#rsync -a ~/dir1 username@remote_host:destination_directory[/console]

This technique "pushes" a local directory to a distant machine. Pull syncs a remote directory with the local system. If dir1 were remote, the syntax would be:

[console]#rsync -a username@remote_host:/home/username/dir1 place_to_sync_on_local_machine[/console]

Like cp and similar tools, source is always first, destination second.

Utilizing Alternative Rsync Methods

Rsync's flag choices can change the utility's default behaviour.

Text files that haven't been compressed can be compressed with the -z option to minimise network transfer.

[console]#rsync -az source destination[/console]

Also useful is -P. It's —progress and —partial. The first flag shows transfer progress, and the second resumes interrupted transfers.

[console]#rsync -azP source destination[/console]

sending incremental file list
created directory destination
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=99/101)
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=98/101)
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=97/101)
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=96/101)
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=95/101)[/console]

If you execute the command once more, you will see a condensed version of the results because no modifications have been made since the last time. This exemplifies Rsync's capability of making use of modification times in order to detect whether or not changes have been made:

[console]#rsync -azP source destination[/console]
sending incremental file list
sent 818 bytes received 12 bytes 1660.00 bytes/sec
total size is 0 speedup is 0.00[/console]

Say you updated several files' modification time with this command:

[console]#touch dir1/file{1..10}[/console]

If you run rsync with -azP again, you'll see how it intelligently recopies just modified files.

[console]#rsync -azP source destination[/console]

sending incremental file list
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=99/101)
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=98/101)
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=87/101)
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=76/101)[/console]

To sync two directories, delete files from the destination if they're removed from the source. rsync doesn't remove files by default.

—delete modifies this behaviour. Before using this option, use -n, —dry-run, to prevent data loss.

[console]#rsync -an --delete source destination[/console]

You can exclude files or directories from a syncing directory by using the —exclude= option and a comma-separated list.

[console]#rsync -a --exclude=pattern_to_exclude source destination[/console]

You can override an exclusion pattern with the —include= option.

[console]#rsync -a --exclude=pattern_to_exclude --include=pattern_to_include source destination

The Rsync —backup option stores backups of essential files. It's used with the —backup-dir option, which defines where backup files go.

[console]#rsync -a --delete --backup --backup-dir=/path/to/backups /path/to/source destination

Rsync simplifies networked file transfers and local directory syncing. Rsync's versatility makes it useful for many file-level tasks.

Mastering Rsync lets you construct elaborate backups and control how and what is shared.

Hope you have understand the above steps To Use Rsync to Sync Local and Remote Directories

Must read:- https://utho.com/docs/tutorial/find-multiple-ways-to-user-account-info-and-login-details-in-linux/



Special Offer

Deploy your Cloud Server now and get $100 in Free Credits!

Get Started
Table of Contents