博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用rsync 的 --delete参数删除目标目录比源目录多余的文件
阅读量:5340 次
发布时间:2019-06-15

本文共 6228 字,大约阅读时间需要 20 分钟。

root@v01 ~]# mkdir dir01 dir02[root@v01 ~]# lsanaconda-ks.cfg  dir02   framework    install.log.syslog  mobiledir01            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 dir01file1A.txt  fileA2.txt  fileA3.txt[root@v01 ~]# ls dir02file1A.txt  fileA2.txt  fileA3.txt  fileB1.txt  fileB2.txt  fileB3.txt[root@v01 ~]#

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

[root@v01 ~]# rsync -avz dir01/* dir02/sending incremental file listfile1A.txtfileA2.txtfileA3.txtsent 166 bytes  received 69 bytes  470.00 bytes/sectotal size is 0  speedup is 0.00

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

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

 失败!!!!

[root@v01 ~]# rsync -avz --delete dir01/ dir02/sending incremental file list./deleting fileB3.txtdeleting fileB2.txtdeleting fileB1.txtsent 71 bytes  received 15 bytes  172.00 bytes/sectotal 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 dir02file1A.txt  fileA2.txt  fileA3.txt  fileB04.txt

 

[root@v01 ~]# rsync -avz --delete dir01/* dir02/*sending incremental file listERROR: destination must be a directory when copying more than 1 filersync 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]# lsfile1A.txt  fileA2.txt  fileA3.txt[root@v01 dir01]# mkdir test[root@v01 dir01]# lsfile1A.txt  fileA2.txt  fileA3.txt  test

 

[root@v01 dir01]# cd ..[root@v01 ~]# lsanaconda-ks.cfg  dir02   framework    install.log.syslog  mobiledir01            ecshop  install.log  localsvn            online

 

[root@v01 ~]# rsync -arvz --exclude="test" --delete dir01/ dir02/sending incremental file list./deleting fileB04.txtsent 75 bytes  received 15 bytes  180.00 bytes/sectotal size is 0  speedup is 0.00

 

[root@v01 ~]# ls dir02/file1A.txt  fileA2.txt  fileA3.txt

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

[root@v01 dir02]# lsfile1A.txt  fileA2.txt  fileA3.txt[root@v01 dir02]# mkdir .test[root@v01 dir02]# lsfile1A.txt  fileA2.txt  fileA3.txt[root@v01 dir02]# ll -alltotal 12drwxr-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.txtdrwxr-xr-x.  2 root root 4096 May 11 08:16 .test

 

[root@v01 ~]# ls dir01file1A.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/sectotal size is 0  speedup is 0.00
[root@v01 ~]# ll -all dir02total 8drwxr-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 12drwxr-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.txtdrwxr-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/sectotal size is 0  speedup is 0.00[root@v01 ~]# ls -all dir02/total 12drwxr-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.txtdrwxr-xr-x.  2 root root 4096 May 11 08:33 .kk

 

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

[root@v01 ~]# mkdir dir{01,02}/.svn[root@v01 ~]# ls -all dir01total 16drwxr-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.txtdrwxr-xr-x.  2 root root 4096 May 11 08:37 .svndrwxr-xr-x.  2 root root 4096 May 11 08:02 test[root@v01 ~]# ls -all dir02total 16drwxr-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.txtdrwxr-xr-x.  2 root root 4096 May 11 08:33 .kkdrwxr-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 listdeleting .kk/test/sent 96 bytes  received 16 bytes  224.00 bytes/sectotal 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 dir01total 16drwxr-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.txtdrwxr-xr-x.  2 root root 4096 May 11 08:39 .svndrwxr-xr-x.  2 root root 4096 May 11 08:02 test[root@v01 ~]# ls -all dir02total 16drwxr-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.txtdrwxr-xr-x.  2 root root 4096 May 11 08:39 .svndrwxr-xr-x.  2 root root 4096 May 11 08:02 test

 

转载于:https://www.cnblogs.com/bass6/p/5504499.html

你可能感兴趣的文章
一个.NET通用JSON解析/构建类的实现(c#)
查看>>
Windows Phone开发(5):室内装修 转:http://blog.csdn.net/tcjiaan/article/details/7269014
查看>>
详谈js面向对象 javascript oop,持续更新
查看>>
关于这次软件以及pda终端的培训
查看>>
jQuery上传插件Uploadify 3.2在.NET下的详细例子
查看>>
如何辨别一个程序员的水平高低?是靠发量吗?
查看>>
新手村之循环!循环!循环!
查看>>
正则表达式的用法
查看>>
线程安全问题
查看>>
下拉刷新
查看>>
linux的子进程调用exec( )系列函数
查看>>
MSChart的研究
查看>>
C# 索引器
查看>>
MySQLdb & pymsql
查看>>
zju 2744 回文字符 hdu 1544
查看>>
delphi 内嵌汇编例子
查看>>
【luogu P2298 Mzc和男家丁的游戏】 题解
查看>>
前端笔记-bom
查看>>
MATLAB作图方法与技巧(一)
查看>>
上海淮海中路上苹果旗舰店门口欲砸一台IMAC电脑维权
查看>>