使用rsync 的 --delete参数删除目标目录比源目录多余的文件
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参数删除目标目录比源目录多余的文件的更多相关文章
- rsync同步时,删除目标目录比源目录多余文件的方法(--delete)
在日常运维工作中,我们经常用到rsync这个同步神器.有时在同步两个目录时,会要求删除目标目录中比源目录多出的文件,这种情况下,就可用到rsync的--delete参数来实现这个需求了. 实例说明:在 ...
- rsync的命令参数【转】
本篇文章,我们只介绍rsync的命令参数. rsync参数的具体解释如下: -v, –verbose 详细模式输出 -q, –quiet 精简输出模式 -c, –checksum 打开校验开关,强制对 ...
- [VBS]带参数删除扩展名不是*.h、*.c、*.cpp的全部文件
脚本使用例程CleanFolder遍历一个文件夹 1)使用CleanFolder递归遍历该文件夹下的所有子文件夹 2)如果该子文件夹的大小为0,则删除这个文件夹 3)遍历该文件夹下的所有文件,扩展名不 ...
- java File delete 无法删除文件的原因。
windows下使用java.io.File.delete()方法删除文件时,返回值为true. 但是本地文件仍然存在,也就是说没有删除成功. 这时候你要检查下你传进来的文件目录格式是否正确. 正确: ...
- HIve:beeline终端上在输错hive语句时,无论 Backspace还是delete 都删除不掉错误的语句,没有办法退格
通过SecureCRT工具连上linux后,通过beeline连接上hive后,在输错hive语句时,无论 Backspace还是delete 都删除不掉错误的语句,没有办法退格. 解决方案: 第一步 ...
- rsync配置文件的参数详解
rsyncd.conf配置文件常用参数说明: rsyncd.conf参数 参数说明 uid=rsync rsync使用的用户. gid=rsync rsync使用的用户组(用户所在的组) use ch ...
- java中File的delete()方法删除文件失败的原因
java中File的delete()方法删除文件失败的原因 学习了:http://hujinfan.iteye.com/blog/1266387 的确是忘记关闭了: 引用原文膜拜一下: 一般来说 ja ...
- (转) Delete/Truncate删除,释放表空间、降低高水位线、resize释放磁盘空间相关优化
硬盘空间不足,打算删除数据库中的多余数据,但删除数据后,硬盘硬盘空间不能释放.[delete后用:alter table table_name move truncate后用:alter tab ...
- linux面试题:删除一个目录下的所有文件,但保留一个指定文件
面试题:删除一个目录下的所有文件,但保留一个指定文件 解答: 假设这个目录是/xx/,里面有file1,file2,file3..file10 十个文件 [root@oldboy xx]# touch ...
随机推荐
- $root knockout
http://www.cnblogs.com/rohelm/p/3209757.html 以列表方式呈现数据 处理以数组形式储存的多条数据,要先认识foreach.在ViewModel定义一个Jav ...
- MongoDB的增删改查 转
MongoDB的增删改查 (黎明你好原创作品,转载请注明) MongoDB中数据的基本单元叫做文档,采用json的键-值的方式.多个键及其关联的值有序的存放在一起变是文档.类似于编程语言中的键值关系. ...
- IE兼容模式下两个小问题,JSON.stringify和SCRIPT70 无权限
JSON.stringify在IE兼容模式下不起作用,原来是序列化对象是一个easyuiTree的树节点对象,过于复杂的对象 SCRIPT70 权限,问题出现在获取页面iframe时: var ifr ...
- Assembly文件被锁定
使用 Assembly.LoadFile 加载程序集后 ,被加载的文件就会被锁定,之后就不能对其执行转移.删除等操作 为了解决次问题,我们可以先读取成字节流,然后转换成Assembly.代码如下:复制 ...
- ecshop 在php5.5上安装错误解决
1.找到ecshop\includes\cls_image.php文件 搜索 function gd_version 改成 static function gd_version 2.Strict St ...
- swift中文文档翻译之--字符串和字符
字符串和字符 A string is an ordered collection of characters, such as "hello, world" or "al ...
- 通过ajax访问aspx的CodeBehind中的方法
引言 在项目中突然看到,aspx中的ajax可以访问aspx.cs中的方法,觉得很新奇,也许是lz少见多怪,不过,真的有发现新大陆似的那种兴奋,你也许知道这代表什么,学会了这种方式,代表你以后,可以建 ...
- Java中的封装
在前面的一些日子里,一只都在学习C#语言,使用C#在做一些小项目的,今天转到了Java的学习,还是感觉有点的不习惯,没有以前的中文界面的,全是英文.写起代码来都一直保持着C#中的编码的习惯,但是学习J ...
- js打印(控件)及多种方式
非常好用的LODOP打印控件 Lodop打印控件简单使用方法 1.安装. 2.调用LodopFuncs.js文件. 3.增加OBJECT对象 <script language="jav ...
- IOS项目删除Git
默认创建工程会在MAC上面创建Git版本管理, 但是呢, 我现在想上传到svn服务器进行管理, 但是已经有个git 好像上传不了 只有把Git删了才能继续. 连问带查, 终于找到解决方案 把 .git ...