阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用rsync 的 –delete参数删除目标目录比源目录多余的文件

175次阅读
没有评论

共计 2332 个字符,预计需要花费 6 分钟才能阅读完成。

最近在处理策划资源文件的时候需要将目录 A 的文件全部同步到目录 B 的文件,并且把目录 B 内多余的文件全部删除掉。所以,就想到了使用 rsync 的 –delete 参数来实现功能。

创建示例如下:

$ mkdir {dirA,dirB}    // 创建两个测试目录

// 分别在两个目录创建相应的文件

$ touch dirA/{fileA1.txt,fileA2.txt,fileA3.txt}

$ touch dirB/{fileA1.txt,fileA2.txt,fileA3.txt,fileB1.txt,fileB2.txt,fileB3.txt}

1) 将 dirA 的所有文件同步到 dirB 内,并保留文件的属主,属组,文件权限等信息。

  $ rsync -avz dirA/ dirB/

sending incremental file list

./

fileA1.txt

fileA2.txt

fileA3.txt

 

sent 199 bytes  received 72 bytes  542.00 bytes/sec

total size is 0  speedup is 0.00

2) 将 dirA 的所有文件同步到 dirB 内,并删除 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  received 72 bytes  550.00 bytes/sec

total size is 0  speedup is 0.00

 

3) 将 dirA 的所有文件同步到 dirB,但是在 dirB 内除了 fileB3.txt 这个文件不删之外,其他的都删除。

$ 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  received 72 bytes  550.00 bytes/sec

total size is 0  speedup is 0.00

4)将 dirA 目录内的 fileA1.txt 和 fileA2.txt 不同步到 dirB 目录内。

$ rsync -avz –exclude=”fileA1.txt” –exclude=”fileA2.txt” dirA/ dirB/

sending incremental file list

fileA3.txt

 

sent 106 bytes  received 31 bytes  274.00 bytes/sec

total size is 0  speedup is 0.00

 

5) 将 dirA 目录内的 fileA1.txt 和 fileA2.txt 不同步到 dirB 目录内,并且在 dirB 目录内删除多余的文件。

$ 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  received 31 bytes  274.00 bytes/sec

total size is 0  speedup is 0.00

6) 将 dirA 目录内的 fileA1.txt 和 fileA2.txt 不同步到 dirB 目录内,并且在 dirB 目录内删除多余的文件,同时,如果 dirB 内有 fileA2.txt 和 fileA1.txt 这两个被排除同步的文件,仍然将其删除。

$ 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  received 34 bytes  286.00 bytes/sec

total size is 0  speedup is 0.00

这里可以看到只有 fileA3.txt 被同步到 dirB 目录内,同时 dirB 目录内的 fileA1.txt 和 fileA2.txt 两个被过滤的文件也被删除掉了。

这里分享一下学习经验,学一个东西不要总是很被动的去学习它有多少功能,要根据自己的实际应用场景来选择自己需要的功能。这样才能印象深刻。

Rsync 的详细介绍 :请点这里
Rsync 的下载地址 :请点这里

推荐阅读:

利用 inotifywait 监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用 inotify+rsync 实现 Linux 文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync 实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync 同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计2332字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中