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

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.

 

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

 

To start with, you need to 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, I 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:

  • -a – means archive mode
  • -v – means verbose, showing details of ongoing operations

 

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

$ rsync -av Documents/* /tmp/documents

 

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

 

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

$ rsync -auv Documents/* /tmp/documents

 

Syncing Files From Local to Remote Linux

 

In the example below, I am copying files from my local machine to a remote server with the IP address – 147.45.10.90. 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/* jarvis@147.45.10.90:~/all/

 

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/* jarvis@147.45.10.90:~/all/
$ rsync -av --update Documents/* jarvis@147.45.10.90:~/all/

 

To update existing files and prevent the 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 users.

Was this answer helpful? 0 Users Found This Useful (0 Votes)