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

[‘

n

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.

n

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.

n

Suggested Read: 10 Practical Examples of Rsync Command in Linux

n

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

n

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.

n

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.

n

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

n

# rsync options source destination rn

n

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

n

Syncing Files Locally Using Rsync

n

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

n

$ rsync -av Documents/* /tmp/documentsrn

n

In the command above, the option:

n

    n

  1. -a – means archive mode
  2. n

  3. -v – means verbose, showing details of ongoing operations
  4. n

n

Sync Files Locally
Sync Files Locally

n

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 a second time:

n

$ rsync -av Documents/* /tmp/documentsrn

n

Sync New Updated Files
Sync New Updated Files

n

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

n

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

n

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.

n

$ rsync -aunv Documents/* /tmp/documentsrn

n

Dry Run Rsync Before Syncing Files
Dry Run Rsync Before Syncing Files

n

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

n

$ rsync -auv Documents/* /tmp/documentsrn

n

Sync Updated Files
Sync Updated Files

n

Syncing Files From Local to Remote Linux

n

In the example below, I am copying files from my local machine to a remote server with the IP address – 10.42.1.5. 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:

n

$ rsync -av --ignore-existing Documents/* [emailxa0protected]:~/all/rn

n

Sync Files Local to Remote Linux
Sync Files Local to Remote Linux

n

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:

n

$ rsync -av --dry-run --update Documents/* [emailxa0protected]:~/all/rn$ rsync -av --update Documents/* [emailxa0protected]:~/all/rn

n

Check Sync Only Updated Files
Check Sync Only Updated Files

n

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

n

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

n

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.

n

‘]