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. java + jquery + ajax + json 交互

    前端js部分: $.ajax({ async:true, cache:false, type:"POST", dataType : 'json', url:"/shopp ...

  2. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  3. anr产生的原理&如何避免(android)

  4. fiddler 挂载 JS文件

    有时候,网站 JS 有问题,或者我们想调试JS,就修改了JS,,然后希望它在本地能与网站一起运行,就需要用到 挂载JS了

  5. jquery发送ajax请求返回数据格式

    jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等. 1.html格式的数据 "<div class='comment ...

  6. ecshop后台admin路径怎么修改

    ecshop后台admin路径怎么修改 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-03-25   ecshop如何修改后台admin路径? 大家都知道ec ...

  7. jdbc链接mysql转

    完整java开发中JDBC连接数据库代码和步骤   JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的 ...

  8. hdu 1257 最少拦截系统(贪心)

    解题思路:[要充分理解题意,不可断章取义] 贪心:每个防御系统要发挥其最大性能, 举例: Input : 9 389 207 155 300 299 170 155 158 65 Output: 2 ...

  9. 10.11 pod 安装

    http://blog.csdn.net/youtk21ai/article/details/48896043

  10. ubuntu安装wiz笔记

    wiz笔记支持跨平台 下面记录一下如何在ubuntu下面安装wiz笔记 1,ubuntu默认是没有wiz资源的,需要先添加官方ppa软件仓库 sudo add-apt-repository ppa:w ...