rsync所有以特定数字开头的目录

时间:2013-02-01 21:22:42

标签: linux unix rsync

我的目录已经加载了数千个子目录:

/home/tmp/
          1
          12
          123
          1234
          2345
          234
          3456
          345
          34

如果第一级子目录匹配,每个子目录依次有数百个我想要rsync的子目录...

我需要的是一种只复制/ rsync以给定数字开头的目录的方法[1-9] ......

我认为我想要的基本上是允许我使用外卡来匹配

rsync -rzvvhP remotehost:/home/tmp/1* /home/tmp/

我希望rsync同步

/home/tmp/1/
/home/tmp/12/
/home/tmp/123/
/home/tmp/1234/

目录及其拥有的任何子目录,但不包含以不同数字开头的任何第一级目录...

/home/tmp/234/
/home/tmp/2345/
........./3*/
........./4*/ etc..

我尝试了什么:

rsync -rzvvhP --exclude='*' --include-from=1.txt remotehost:/home/tmp/ /home/tmp/

其中1.txt包含:

1
12
123
1234

当我使用2.txt执行此操作时虽然rsync似乎仍然运行所有以1和3等开头的目录...

我刚试过:

rsync -rzvvhP --include-from=2.txt remoteaccount@remotehost:/home/tmp/ /home/tmp/
first I get....
[sender] showing directory 2758 because of pattern 2758

......这似乎很好,但后来我得到......

1014/135954/T33-2.JPG is uptodate
1014/135956/456309.jpg is uptodate

......这是不好的

根据我尝试过的建议:

rsync -rzvvhP --size-only --filter='+ /2*/ - /*' remoteuser@remotehost:/home/tmp/ /home/tmp/ 

当它运行时,我得到的第一件事是一堆“uptodate”行文件,不应该是这个rsync的一部分:

1001/149984/T3201-2.JPG is uptodate

我该如何做到这一点,以便我可以使用一个命令来仅rsync以任何给定数字开头的目录?

2 个答案:

答案 0 :(得分:2)

创建过滤器文件,如下所示:

/my/dir/include.txt

+ /1*/
+ /3*/

# Skip everything else
- /*

现在将此添加到rsync

rsync -rzvvhP \
    --filter='merge /my/dir/include.txt' \
    remotehost:/home/tmp/ /home/tmp/

答案 1 :(得分:0)

我很确定我错过了一些东西,因为你说

  

我认为我想要的基本上是允许我使用外卡来匹配

rsync -rzvvhP remotehost:/home/tmp/1* /home/tmp/

但这只是起作用。

让我们看看远程主机上的内容。

$ ssh remotehost find /tmp/a -type f
/tmp/a/123/x/a
/tmp/a/123/x/c
/tmp/a/123/y/b
/tmp/a/12/x/a
/tmp/a/12/x/c
/tmp/a/12/y/b
/tmp/a/1/x/a
/tmp/a/1/x/c
/tmp/a/1/y/b
/tmp/a/23456/x/a
/tmp/a/23456/x/c
/tmp/a/23456/y/b
/tmp/a/234/x/a
/tmp/a/234/x/c
/tmp/a/234/y/b
/tmp/a/34567/x/a
/tmp/a/34567/x/c
/tmp/a/34567/y/b
/tmp/a/3456/x/a
/tmp/a/3456/x/c
/tmp/a/3456/y/b
/tmp/a/345/x/a
/tmp/a/345/x/c
/tmp/a/345/y/b
/tmp/a/34/x/a
/tmp/a/34/x/c
/tmp/a/34/y/b
/tmp/a/3/x/a
/tmp/a/3/x/c
/tmp/a/3/y/b

现在rsync只有以2开头的目录。根据你的shell,你可能需要引用通配符,就像我在这里一样,或者不需要这样做。在bash下,这不是必要的。

$ rsync -rzvvhP 'remotehost:/tmp/a/2*' .
opening connection using: ssh remotehost rsync --server --sender -vvrze.iLsf . "/tmp/a/2*"
receiving incremental file list
delta-transmission enabled
234/
234/x/
234/x/a
       8 100%    7.81kB/s    0:00:00 (xfer#1, to-check=7/12)
234/x/c
       8 100%    7.81kB/s    0:00:00 (xfer#2, to-check=6/12)
234/y/
234/y/b
       8 100%    7.81kB/s    0:00:00 (xfer#3, to-check=5/12)
23456/
23456/x/
23456/x/a
       8 100%    7.81kB/s    0:00:00 (xfer#4, to-check=2/12)
23456/x/c
       8 100%    7.81kB/s    0:00:00 (xfer#5, to-check=1/12)
23456/y/
23456/y/b
       8 100%    7.81kB/s    0:00:00 (xfer#6, to-check=0/12)
total: matches=0  hash_hits=0  false_alarms=0 data=48

sent 149 bytes  received 533 bytes  454.67 bytes/sec
total size is 48  speedup is 0.07