How to Use Rsync to Sync New or Changed/Modified Files in Linux

As a system administrator or Linux power user, you may have probably come across or even on several occasions, used the versatile Linux Rsync tool, which enables users to expeditiously copy or synchronize files locally and remotely. It is as well a great tool popularly used for backup operations and mirroring.

Some of its eminent features and advantages include; it is exceptionally versatile in that, it can copy locally, to/from a remote shell or remote rsync, it is also remarkably flexible, allowing users to specify any number of files to copy.

Suggested Read: 10 Practical Examples of Rsync Command in Linux

Furthermore, it permits copying of links, devices, file or directory owner, groups and the permissions. It also supports usage without root privileges coupled with many more.

One imperative differential of rsync in comparison to other file-coying commands in Linux is its use of the remote-update protocol, to transfer only the difference between files or directory content.

Therefore, in this article, we shall examine how rsync can help us only sync new or changed files or directory content while making backups and beyond in Linux.

To start with, you need remember that the conventional and simplest form of using rsync is as follows:

# rsync options source destination 

That said, let us dive into some examples to uncover how the concept above actually works.

Syncing Files Locally Using Rsync

Using the command below, am able to copy files from my Documents directory to /tmp/documents directory locally:

$ rsync -av Documents/* /tmp/documents

In the command above, the option:

  1. -a – means archive mode
  2. -v – means verbose, showing details of ongoing operations
Sync Files Locally

Sync Files Locally

By default, rsync only copies new or changed files from a source to destination, when I add a new file into my Documents directory, this is what happens after running the same command second time:

$ rsync -av Documents/* /tmp/documents
Sync New Updated Files

Sync New Updated Files

As you can observe and notice from the output of the command, only the new file is copied to the destination directory.

Suggested Read: How to Sync Two Apache Web Servers/Websites Using Rsync

The --update or -u option allows rsync to skip files that are still new in the destination directory, and one important option, --dry-run or -n enables us to execute a test operation without making any changes. It shows us what files are to be copied.

$ rsync -aunv Documents/* /tmp/documents
Dry Run Rsync Before Syncing Files

Dry Run Rsync Before Syncing Files

After executing a test run, we can then do away with the -n and perform a real operation:

$ rsync -auv Documents/* /tmp/documents
Sync Updated Files

Sync Updated Files

Syncing Files From Local to Remote Linux

In the example below, I am copying files from my local machine to a remote sever with the IP address – So as to only sync new files on the local machine, that do not exist on the remote machine, we can include the --ignore-existing option:

$ rsync -av --ignore-existing Documents/* [email protected]:~/all/
Sync Files Local to Remote Linux

Sync Files Local to Remote Linux

Subsequently, to sync only updated or modified files on the remote machine that have changed on the local machine, we can perform a dry run before copying files as below:

$ rsync -av --dry-run --update Documents/* [email protected]:~/all/
$ rsync -av --update Documents/* [email protected]:~/all/
Check Sync Only Updated Files

Check Sync Only Updated Files

To update existing files and prevent creation of new files in the destination, we utilize the --existing option.

You can run through the rsync man page to discover additionally useful options for advanced usage, as I had mentioned earlier on, rsync is a very powerful and versatile Linux tool and many System Administrator and Linux power users know just how advantageous it is.

Most importantly, you can as well share your view on the examples we have covered here or even better still, offer us valuable tips on using this vital command line tool through the comment section below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

4 Responses

  1. Imteyaz says:

    Thanks for writing this post.

  2. Mark says:

    Love this, thanks Aaron, I’m fairly new to Linux myself and these articles help me tremendously with enhancing my skills

Leave a Reply to Mark Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.