How to Sync New and Changed Files Using ‘rsync’ Command

As a system administrator or Linux power user, you may have probably come across or even on several occasions, used the versatile Linux rsync command, 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 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 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, being 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.
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 a 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.

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 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:

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 the creation of new files in the destination, we utilize the --existing option.

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.

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.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

13 Comments

Leave a Reply
  1. Hello, I have a routine with rsync to backup remote folders, however, after reboot, I needed to mount my remote folders again when I run my rsync show one error.

    rsync: change_dir "/mnt/REMOTEFOLDER" failed: No such file or directory (2)

    What happened?

    Thanks.

    Reply
  2. Hi! When I use:

    # rsync -av /mapped_drive/src_folder/ /mylocal/dest_folder
    

    I get to have a new folder named “dest_folder” inside /mylocal/dest_folder that starts to get filled with all files from /src_folder.

    What I would need is that the /mylocal/dest_folder (that has already some files from the /src_folder) gets the rest of the files. Is something wrong with my line of code? When changing it to have also the –-ignore-existing, it still continues to fill the /mylocal/dest_folder/dest_folder and not the /mylocal/dest_folder.

    Thanks in advance!

    Reply
  3. I have made a script-based app that helps with backing up files to remote server / smb share – https://github.com/jamieduk/LanBackup_Full.

    but would like it to update files and ignore if files are the same can you look at backup.sh inside the web part of the project and advise the best command to use to update files that have changed and ignore files of the same size?

    Reply
  4. Hi, Thanks for this. I use Rsync to backup my unraid photos and videos to my local raspberry pi external HDD. Photos run fine and checks for files already in the drive and only copy’s new files however videos seem to apart to copy the same file already on the drive as if they were new and I run out of disk space.

    Any ideas on how I can have rclone check what’s already on the drive and only copy the new files. My video script is the same as my photos script so. It sure why it feels the need to copy the files again.

    Thanks.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.