How to update new files with rsync

rsync is a command-line tool used for the synchronization of directories and files between two systems. It supports syncing files, directories, devices, file and directory owners, groups, and permissions. The remote-update protocol that transfers the change between directories or file content distinguishes rsync from other file synchronization commands in Linux.

I recently needed to utilize rsync to copy and update the new files that did not already exist at the destination system or directory. Are you also willing to use rsync for only updating and transferring new files? Then follow the rest of the article to learn how to do it.

After the file synchronization through rsync, the process of updating newer files consists of the following steps:

  1. Ignoring existing files
  2. The dry run update process
  3. Update newer files

Now, we will show you two methods following the procedure mentioned above.

Method 1: Updating newer files locally with rsync

For synchronization files from source to the destination directory, check out the below-given command. In our case, “testdir1” is our source and “testdir2” is the destination folder.

$ rsync -av testdir1/ testdir2/

The “-u” or the “–update” option forces rsync to skip that files in your destination directory that are still new, and the “-n” or the “–dry run” option is for testing the update process.

$ rsync -aunv testdir1/ testdir2/

Now, remove the “-n” option and quickly update the rsync command execution.

$ rsync -auv testdir1/ testdir2/

You can see in the retrieved output that only the “testfile” is added to the destination directory this time because it was added to the source file after the file syncing process, which makes it a “new file” for the rsync command.

Method 2: Updating newer files from local to the remote system

–ignore-existing option in rysnc command forces rsync to ignore the files update already existing on the destination. Usage of “–ignore-existing” will make sure that the files already been handled do not get change. It means that the “–ignore-existing” will only look at the already existing files present in the destination hierarchy.

$ sudo rsync –ignore-existing -raz –progress  testdir1/ linuxhint@10.0.2.15:testdirectory2/

Let’s suppose you made some file creation changes in your local system after syncing files, and you want only to update the newer files to the destination directory. For this, test a dry update run by utilizing the below-given command:

$ sudo rsync -av –dry-run –update testdir1/ linuxhint@10.0.2.15:testdirectory2/

Now you can move towards the actual update of newer files.

$ sudo rsync -av –update testdir1/ linuxhint@10.0.2.15:testdirectory2/

The output declares that “TESFOLDER” is considered a newer file and is updated to the remote system’s directory.

Conclusion:

In any system, updating only the newer files removes the data redundancy. rsync command provides this facility in a Linux-based system. This process consists of the following steps: file synchronization, ignoring existing files, test dry run, and, lastly, the actual execution of the update process. We have provided you two methods for updating the newer files in your destination directory in this post.