How To Use Rsync To Sync Local Directories

Introduction

Rsync, which stands for “remote sync”, is a remote and local file synchronization tool. It uses an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed.

What Is Rsync?

Rsync is a very flexible network-enabled syncing tool. Due to its ubiquity on Linux and Unix-like systems and its popularity as a tool for system scripts, it is included on most Linux distributions by default.

Basic Syntax

The basic syntax of rsync is very straightforward, and operates in a way that is similar to ssh, scp, and cp.

We will create two test directories and some test files with the following commands:

cd ~
mkdir dir1
mkdir dir2
touch dir1/file{1..100}

We now have a directory called dir1 with 100 empty files in it.

ls dir1

We also have an empty directory called dir2.

To sync the contents of dir1 to dir2 on the same system, type:

rsync -r dir1/ dir2

Lets list the dir2 and see whether the files copied to directory

ls dir2

An Important Note

You may have noticed that there is a trailing slash (/) at the end of the first argument in the above command.

This is necessary to mean “the contents of dir1”. The alternative, without the trailing slash, would place dir1, including the directory, within dir2. This would create a hierarchy that looks like:

~/dir2/dir1/[files]

Always double-check your arguments before executing an rsync command.

We hope you’ve found this useful!