Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use rsync to compare and synchronize files under Linux

Shulou Source: shulou.com Published: 2022-06-01 15:51:48 09月13日 Update

This article mainly explains "how to use rsync to compare and synchronize files under Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use rsync to compare and synchronize files under Linux.

Exclude file and directory lists

Sometimes, when we do a lot of synchronization, we may want to exclude a list of files and directories from the synchronized files and directories. Generally speaking, it is not appropriate to synchronize files such as device files and certain system files, or files that take up unnecessary disk space, such as temporary files or cache files, which we need to exclude.

First, let's create a file called "excluded" (you can name it whatever you want, of course), and then write the folder or file we want to exclude to it, one line at a time. In our example, if you want to make a full backup of the root partition, you should exclude some of the device directories created at startup and the directories where temporary files are placed. The list looks like this:

You can then run the following command to back up the system:

The code is as follows:

$sudo rsync-aAXhv-exclude-from=excluded / / mnt/backup

Exclude files from the command line

You can also exclude files directly from the command line, which is useful when you want to exclude a small number of files and when you want to script it or add it to crontab without script or cron relying on another file to run.

For example, if you want to synchronize / var to a backup directory, but you don't want to contain folders such as cache and tmp, which usually don't have important content, you can use the following command:

The code is as follows:

$sudo rsync-aAXhv-- exclude= {"/ var/cache", "/ var/tmp"} / var/ home/adrian/var

This command is easy to use in scripts or cron and does not depend on other files.

At this point, I believe you have a deeper understanding of "how to use rsync to compare and synchronize files under Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Files synchronization directories commands contents backups scripts code folders methods systems devices learning running appropriate practical deeper important generally speaking one line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Tech Info Linux Docker Apple