root@v01 ~]# mkdir dir01 dir02
[root@v01 ~]# ls
anaconda-ks.cfg dir02 framework install.log.syslog mobile
dir01 ecshop install.log localsvn online
[root@v01 ~]# touch dir02/{file1A.txt,fileA2.txt,fileA3.txt,fileB1.txt,fileB2.txt,fileB3.txt}
[root@v01 ~]# touch dir01/{file1A.txt,fileA2.txt,fileA3.txt}
[root@v01 ~]# ls dir01
file1A.txt fileA2.txt fileA3.txt
[root@v01 ~]# ls dir02
file1A.txt fileA2.txt fileA3.txt fileB1.txt fileB2.txt fileB3.txt
[root@v01 ~]#

将dir01的所有文件同步到dir02内,并保留文件的属主,属组,文件权限等信息

[root@v01 ~]# rsync -avz dir01/* dir02/
sending incremental file list
file1A.txt
fileA2.txt
fileA3.txt sent 166 bytes received 69 bytes 470.00 bytes/sec
total size is 0 speedup is 0.00

将dir01的所有文件同步到dirB内,并删除dir02内多余的文件:

[root@v01 ~]# rsync -avz --delete dir01/* dir02/
sending incremental file list sent 58 bytes received 12 bytes 140.00 bytes/sec
total size is 0 speedup is 0.00

失败!!!!

[root@v01 ~]# rsync -avz --delete dir01/ dir02/
sending incremental file list
./
deleting fileB3.txt
deleting fileB2.txt
deleting fileB1.txt sent 71 bytes received 15 bytes 172.00 bytes/sec
total size is 0 speedup is 0.00
[root@v01 ~]# ls dir02/
file1A.txt fileA2.txt fileA3.txt

成功!!!!

[root@v01 ~]# touch dir02/fileB04.txt
[root@v01 ~]# ls dir02
file1A.txt fileA2.txt fileA3.txt fileB04.txt
[root@v01 ~]# rsync -avz --delete dir01/* dir02/*
sending incremental file list
ERROR: destination must be a directory when copying more than 1 file
rsync error: errors selecting input/output files, dirs (code 3) at main.c(542) [receiver=3.0.6]
rsync: connection unexpectedly closed (9 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]

将dir01目录内的test目录不同步到dir02目录内

[root@v01 dir01]# ls
file1A.txt fileA2.txt fileA3.txt
[root@v01 dir01]# mkdir test
[root@v01 dir01]# ls
file1A.txt fileA2.txt fileA3.txt test
[root@v01 dir01]# cd ..
[root@v01 ~]# ls
anaconda-ks.cfg dir02 framework install.log.syslog mobile
dir01 ecshop install.log localsvn online
[root@v01 ~]# rsync -arvz --exclude="test" --delete dir01/ dir02/
sending incremental file list
./
deleting fileB04.txt sent 75 bytes received 15 bytes 180.00 bytes/sec
total size is 0 speedup is 0.00
[root@v01 ~]# ls dir02/
file1A.txt fileA2.txt fileA3.txt

同步的同时也包含隐藏文件:

[root@v01 dir02]# ls
file1A.txt fileA2.txt fileA3.txt
[root@v01 dir02]# mkdir .test
[root@v01 dir02]# ls
file1A.txt fileA2.txt fileA3.txt
[root@v01 dir02]# ll -all
total 12
drwxr-xr-x. 3 root root 4096 May 11 08:16 .
dr-xr-x---. 12 root root 4096 May 11 07:39 ..
-rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
drwxr-xr-x. 2 root root 4096 May 11 08:16 .test
[root@v01 ~]# ls dir01
file1A.txt fileA2.txt fileA3.txt test
[root@v01 ~]# rsync -arvz --exclude="test" --delete dir01/ dir02/
sending incremental file list
./
deleting .test/ sent 75 bytes received 15 bytes 180.00 bytes/sec
total size is 0 speedup is 0.00
[root@v01 ~]# ll -all dir02
total 8
drwxr-xr-x. 2 root root 4096 May 11 08:02 .
dr-xr-x---. 12 root root 4096 May 11 07:39 ..
-rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
[root@v01 ~]# mkdir dir02/.kk
[root@v01 ~]# ls -all dir02/
total 12
drwxr-xr-x. 3 root root 4096 May 11 08:26 .
dr-xr-x---. 12 root root 4096 May 11 07:39 ..
-rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
drwxr-xr-x. 2 root root 4096 May 11 08:26 .kk

将dir01的所有文件除test之外 同步到 dir02,但是在dir02内除了.kk 这个文件不删之外,其他的都删除

[root@v01 ~]# rsync -arvz --exclude="test" --exclude=.kk --delete dir01/ dir02/
sending incremental file list
./ sent 75 bytes received 15 bytes 180.00 bytes/sec
total size is 0 speedup is 0.00
[root@v01 ~]# ls -all dir02/
total 12
drwxr-xr-x. 3 root root 4096 May 11 08:02 .
dr-xr-x---. 12 root root 4096 May 11 07:39 ..
-rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
drwxr-xr-x. 2 root root 4096 May 11 08:33 .kk

实现除了.svn之外保持文件同步

[root@v01 ~]# mkdir dir{01,02}/.svn
[root@v01 ~]# ls -all dir01
total 16
drwxr-xr-x. 4 root root 4096 May 11 08:37 .
dr-xr-x---. 12 root root 4096 May 11 07:39 ..
-rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
drwxr-xr-x. 2 root root 4096 May 11 08:37 .svn
drwxr-xr-x. 2 root root 4096 May 11 08:02 test
[root@v01 ~]# ls -all dir02
total 16
drwxr-xr-x. 4 root root 4096 May 11 08:37 .
dr-xr-x---. 12 root root 4096 May 11 07:39 ..
-rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
drwxr-xr-x. 2 root root 4096 May 11 08:33 .kk
drwxr-xr-x. 2 root root 4096 May 11 08:37 .svn
[root@v01 ~]# touch dir01/.svn/test1
[root@v01 ~]# touch dir02/.svn/test2
[root@v01 ~]# rsync -arvz --exclude=".svn" --delete dir01/ dir02/sending incremental file list
deleting .kk/
test/ sent 96 bytes received 16 bytes 224.00 bytes/sec
total size is 0 speedup is 0.00
[root@v01 ~]# ls dir02/.svn/test2
[root@v01 ~]# ls dir01/.svn/test1
dir01/.svn/test1
[root@v01 ~]# ls -all dir01
total 16
drwxr-xr-x. 4 root root 4096 May 11 08:37 .
dr-xr-x---. 12 root root 4096 May 11 07:39 ..
-rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
drwxr-xr-x. 2 root root 4096 May 11 08:39 .svn
drwxr-xr-x. 2 root root 4096 May 11 08:02 test
[root@v01 ~]# ls -all dir02
total 16
drwxr-xr-x. 4 root root 4096 May 11 08:37 .
dr-xr-x---. 12 root root 4096 May 11 07:39 ..
-rw-r--r--. 1 root root 0 May 11 07:42 file1A.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA2.txt
-rw-r--r--. 1 root root 0 May 11 07:42 fileA3.txt
drwxr-xr-x. 2 root root 4096 May 11 08:39 .svn
drwxr-xr-x. 2 root root 4096 May 11 08:02 test

使用rsync 的 --delete参数删除目标目录比源目录多余的文件的更多相关文章

  1. rsync同步时,删除目标目录比源目录多余文件的方法(--delete)

    在日常运维工作中,我们经常用到rsync这个同步神器.有时在同步两个目录时,会要求删除目标目录中比源目录多出的文件,这种情况下,就可用到rsync的--delete参数来实现这个需求了. 实例说明:在 ...

  2. rsync的命令参数【转】

    本篇文章,我们只介绍rsync的命令参数. rsync参数的具体解释如下: -v, –verbose 详细模式输出 -q, –quiet 精简输出模式 -c, –checksum 打开校验开关,强制对 ...

  3. [VBS]带参数删除扩展名不是*.h、*.c、*.cpp的全部文件

    脚本使用例程CleanFolder遍历一个文件夹 1)使用CleanFolder递归遍历该文件夹下的所有子文件夹 2)如果该子文件夹的大小为0,则删除这个文件夹 3)遍历该文件夹下的所有文件,扩展名不 ...

  4. java File delete 无法删除文件的原因。

    windows下使用java.io.File.delete()方法删除文件时,返回值为true. 但是本地文件仍然存在,也就是说没有删除成功. 这时候你要检查下你传进来的文件目录格式是否正确. 正确: ...

  5. HIve:beeline终端上在输错hive语句时,无论 Backspace还是delete 都删除不掉错误的语句,没有办法退格

    通过SecureCRT工具连上linux后,通过beeline连接上hive后,在输错hive语句时,无论 Backspace还是delete 都删除不掉错误的语句,没有办法退格. 解决方案: 第一步 ...

  6. rsync配置文件的参数详解

    rsyncd.conf配置文件常用参数说明: rsyncd.conf参数 参数说明 uid=rsync rsync使用的用户. gid=rsync rsync使用的用户组(用户所在的组) use ch ...

  7. java中File的delete()方法删除文件失败的原因

    java中File的delete()方法删除文件失败的原因 学习了:http://hujinfan.iteye.com/blog/1266387 的确是忘记关闭了: 引用原文膜拜一下: 一般来说 ja ...

  8. (转) Delete/Truncate删除,释放表空间、降低高水位线、resize释放磁盘空间相关优化

    硬盘空间不足,打算删除数据库中的多余数据,但删除数据后,硬盘硬盘空间不能释放.[delete后用:alter table table_name move    truncate后用:alter tab ...

  9. linux面试题:删除一个目录下的所有文件,但保留一个指定文件

    面试题:删除一个目录下的所有文件,但保留一个指定文件 解答: 假设这个目录是/xx/,里面有file1,file2,file3..file10 十个文件 [root@oldboy xx]# touch ...

随机推荐

  1. Spring学习7-Spring整合Hibernate

    一.Springl为什么要整合Hibernate   二者的整合主要是把hibernate中核心的一些类型交给spring管理,这些类型主要包括sessionFactory. transactionM ...

  2. linux建立用户 详细

    .你同时属于两个或两个以上的组. 两个条件你至少具备一个,你才能够把文件所属旧组变为新组.使用如下的命令将当前目录下所有html文件所属的组改为httpd: chgrp httpd *.html 和c ...

  3. Docker系列之(三):Docker微容器Alpine Linux

    1. 前言 使用Docker创建容器时,基础镜像通常选择Ubuntu或Centos,不管哪个镜像的大小都在100MB以上. Alpine Linux是一个面向安全的轻型的Linux发行版. Alpin ...

  4. C语言or和and运算

    #include <stdio.h> void main () { printf( | ); printf( | ); printf( | ); printf( | ); printf( ...

  5. ssh项目删除

    1.在页面添加删除跳转 <td><a href="deleteStandard.action?id=${sta.id }">删除</a>< ...

  6. 面向对象分析设计-------02UML+UML各种图形及作用

    一.UML是什么?UML有什么用? 二.UML的历史 三.UML的上层结构(Superstructure) 四.UML建模工具 五.UML的图(重点) 1.用例图(use case diagram) ...

  7. linux ls和 ll 命令

    工作中用到      ll -alrth|tail -30    命令 所以再来回顾一下 ls 命令 linux ls和 ll 命令 ll 命令列出的信息更加详细,有时间,是否可读写等信息 ll命令和 ...

  8. 【分享】4款WiFi广告营销利器推荐

    随着WiFi的普及,相信大家已经发现很多公共场合连接WiFi的时候会要求登录,这个就是所谓的WiFi广告了. 什么是WiFi广告呢?(以下引用百科)       WiFi广告是目前WiFi营销的主要方 ...

  9. FineUI第八天----下拉列表控件

    下拉列表控件 3.模拟树的下拉列表: 其他的控件都跟Asp.net的差不多.

  10. HDU3348(贪心求硬币数

    ;} coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...