Home  >  Article  >  System Tutorial  >  Guide to rsync usage for excluding files and directories

Guide to rsync usage for excluding files and directories

WBOY
WBOYforward
2024-01-02 19:05:25845browse
Introduction rsync is a very useful and popular Linux tool. It is used to back up and restore files, as well as compare and synchronize files. We have already described how to use rsync under Linux in previous articles, and today we will add some more useful rsync tips.

Guide to rsync usage for excluding files and directories

Exclude files and directories list

Sometimes, when we do a lot of synchronization, we may want to exclude a list of files and directories from being synchronized. Generally speaking, files that occupy unnecessary disk space, such as device files and certain system files, or files that occupy unnecessary disk space, such as temporary files or cache files, are not suitable for synchronization. We need to exclude such files.

First, let's create a file called "excluded" (of course, you can name it whatever you want), and then write the folders or files we want to exclude into the file, one per line. In our example, if you want to make a complete backup of the root partition, you should exclude some device directories created at boot and directories where temporary files are placed. The list will look like this:
Guide to rsync usage for excluding files and directories
Then, you can run the following command to back up your system:

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

Guide to rsync usage for excluding files and directories

Exclude files from command line

You can also exclude files directly from the command line. This method is used when the number of files you want to exclude is small, and when you want to write it into a script or add it to crontab and do not want the script or cron to depend on another file to run. Very useful at times.

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

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

Guide to rsync usage for excluding files and directories
This command is easy to use in scripts or cron and does not depend on other files.

The above is the detailed content of Guide to rsync usage for excluding files and directories. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete