Use the -- delete parameter of rsync to delete unnecessary files in the target directory than in the source directory.

Source: Internet
Author: User

Recently, when processing planning resource files, you need to synchronize all the files in directory A to the files in directory B and delete all the unnecessary files in directory B. Therefore, you can use the -- delete parameter of rsync to implement the function.

Example:
// Create two test Directories
$ Mkdir {Dira, dirb}

// Create the corresponding files in the two directories respectively
$ Touch Dira/unzip filea1.txt,filea2.txt,filea3.txt}
$ Touch dirb/filea1.txt,filea2.txt,filea3.txt,fileb1.txt,fileb2.txt,fileb3.txt}

1) synchronize all Dira files to dirb, and retain the owner, group, and file permissions of the files.

$ Rsync-avz Dira/dirb/

Sending incremental file list
./
Filea1.txt
Filea2.txt
Filea3.txt

Sent 199 bytes encoded ed 72 bytes 542.00 Bytes/sec
Total size is 0 speedup is 0.00

2) synchronize all Dira files to dirb and delete unnecessary files in dirb.
$ Rsync-avz -- delete Dira/dirb/

Sending incremental file list
./
Deleting fileb3.txt
Deleting fileb2.txt
Deleting fileb1.txt
Filea1.txt
Filea2.txt
Filea3.txt
Sent 203 bytes encoded ed 72 bytes 550.00 Bytes/sec
Total size is 0 speedup is 0.00

3. Synchronize all dira's files to dirb, but delete all files except fileb3.txt in dirb.
$ Rsync-avz -- delete -- exclude "fileb3.txt" Dira/dirb/

Sending incremental file list
./
Deleting fileb2.txt
Deleting fileb1.txt
Filea1.txt
Filea2.txt
Filea3.txt

Sent 203 bytes encoded ed 72 bytes 550.00 Bytes/sec
Total size is 0 speedup is 0.00

In step 4, The filea1.txtand filea2.txt files in the diradirectory are not synchronized to the dirb directory.
$ Rsync-avz -- exclude = "filea1.txt" -- exclude = "filea2.txt" Dira/dirb/

Sending incremental file list
Filea3.txt

Sent 106 Bytes encoded ed 31 bytes 274.00 Bytes/sec
Total size is 0 speedup is 0.00

5) The filea1.txtand filea2.txt files in the diradirectory are not synchronized to the dirb directory, and redundant files are deleted from the dirb directory.
$ Rsync-avz -- exclude = "filea1.txt" -- exclude = "filea2.txt" -- delete Dira/dirb/

Sending incremental file list
Deleting fileb3.txt
Deleting fileb2.txt
Deleting fileb1.txt
Filea3.txt
Sent 106 Bytes encoded ed 31 bytes 274.00 Bytes/sec
Total size is 0 speedup is 0.00

The two excluded files are deleted.

$ Rsync-avz -- exclude = "filea1.txt" -- exclude = "filea2.txt" -- delete-excluded Dira/dirb/

Sending incremental file list
./
Deleting fileb3.txt
Deleting fileb2.txt
Deleting fileb1.txt
Deleting filea2.txt
Deleting filea1.txt
Filea3.txt

Sent 109 bytes encoded ed 34 bytes 286.00 Bytes/sec
Total size is 0 speedup is 0.00

Here, we can see that only filea3.txtis synchronized to the dirbdirectory. At the same time, filea1.txtand filea2.txt files in the dirbdirectory are also deleted.

Here, I would like to share my learning experience. Do not always learn how many functions are available when learning something. Select the functions you need based on your actual application scenarios. In this way, you will be impressed.

 

Http://www.linuxidc.com/Linux/2014-03/98835.htm

Use the -- delete parameter of rsync to delete unnecessary files in the target directory than in the source directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.