使用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 ...
随机推荐
- 清理一下电脑垃圾,打开Eclipse发现左边的全部项目消失了
使用360清理一下电脑垃圾,打开Eclipse发现左边的全部项目消失了,但在对应的workspace可以找到项目,这个问题已经是第三次遇到了(估计是被360清理掉Eclipse的一些部署或配置文件所导 ...
- 转 vagrant package[打包命令]详解
转 vagrant package[打包命令]详解 vagrant的一个非常重要的功能就是在你的同事之间分享你的box从而使大家的开发环境保持同步,打包[package]正是实现这一功能的关键所在 ...
- thinkphp 3.2 单入口 多模块 不能加载index控制器问题
菜鸟一个,大神不用看,很喜欢单入口 多模块的方式,所以想自己设置下,结果看很多教程没看懂,也看到有人在问这个问题,分享下我的项目名称是app,首先运行官方的index.php文件,app目录下生成了三 ...
- Jsonp简单认识(后端使用的是asp.net mvc)
一.Jsonp简介:由于浏览器基于安全有同源策略(同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性)机制,所以前端无法使用Ajax来获取来获取其他域名下返回的数据,而Jsonp可 ...
- Applying Eigenvalues to the Fibonacci Problem
http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...
- import numpy 和 from numpy import * 的区别
对于 import xxx 使用函数的方法为 x'x'x.Afunc 而对于 from xxx import * 调用函数的方法为 Afunc
- WPF PopupNonTopmost重写
之前做WPF遇到问题,在网上找到的一个类 public class PopupNonTopmost : System.Windows.Controls.Primitives.Popup { publi ...
- 织梦dedecms如何快速使用拼音首字母做栏目名称
织梦默认使用拼音为保存目录的时候使用的是中文全拼,当遇到栏目名称比较长的时候目录名称看起来有点冗长,这时候大多数站长喜欢使用拼音首字母作为栏目的保存目录,那么有没有什么快速的办法能让我们快速的使用首字 ...
- 关于Eclipse部署openfire3.8.2源码的体会
因为公司要做人际银行的一个项目需要openfire(服务器)+asmack(客户端),所以需要对消息的推送及消息发送知识的积累.所以需要研究xmpp,以前不是很了解这个技术,现在需要学习.首先就得部署 ...
- Linux文件权限管理(持续更新)
文章是从我的个人博客上粘贴过来的, 大家也可以访问我的主页 www.iwangzheng.com 以root身份登录linux以后, ls -al 可以看到 -rw-rw-r-- 1 wangzhe ...