使用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 ...
随机推荐
- xml 嵌入式资源
使用Ibatis总是说未能加载相应的sqlmap.xml,原来是 xml以内容方式,而不是嵌入式方式载入Dll中
- js中日历的代码
Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- CSS小记
1.元素居中 (1)水平居中:指定宽度,然后margin auto 即可 .middle{ max-width:400px; //width:400px;//当浏览器被缩小,宽度小于元素宽度时,元素会 ...
- WebSphere SSLC0008E 无法初始化 SSL 连接。未授权访问被拒绝,或者安全性设置已到期 解决方法
昨天安装websphere服务器中间件,安装完毕之后,安装验证如下: 猜测是SSL协议版本过低的问题,于是打开IE高级设置: 勾线之后,启动管理控制台: 成功启动web界面如下: 登陆试试:
- linux 定时 svn 代码更新,配置文件不修改
普通参数: 普通参数为正常的传参数: 例子: f1("111") 指定参数: 指定参数为指定哪个参数给函数方法里面某个形式参数专用,优点:不受传参数的位置约束. 例子: ...
- apache mysql 正常启动 打开php网页延时
金山毒霸或者升级精灵修改了WINSOCK导致的.由于我电脑上也安装了金山毒霸,而且最近几天也升级过了,应该是同样的问题.于是搜索到恢复Winsock的方法: netsh winsock reset 使 ...
- 理解模板引擎Razor 的原理(转载)
Razor是ASP.NET MVC 3中新加入的技术,以作为ASPX引擎的一个新的替代项.简洁的语法与.NET Framework 结合,广泛应用于ASP.NET MVC 项目.Razor Pad是一 ...
- 基于TcpListener的web服务器
写在前面 上篇文章根据<asp.net 本质论>书上提供的例子,实现了一个简单的web服务器,本篇文章将介绍另一种实现方式——基于TcpListener的web服务器. TcpListen ...
- LINUX中,Vi编辑器的几种模式及保存、退出等命令
vi编辑器有三种模式: 命令模式,编辑模式,末行模式 打开vi后首先是命令模式,用i,o,a等进入编辑模式,按esc退出编辑模式,回到命令模式. 在命令模式下输入:wq表示保存退出,:wq!强制保存退 ...
- show processlist 其中status详解(适用于所有概况)
mysql show processlist分析 2011-04-11 16:13:00 分类: Mysql/postgreSQL mysql> show processlist; +—–+—— ...