简体   繁体   English

如何通过保持目录结构完整来rsync路径中具有匹配模式的文件?

[英]How to rsync files with matching pattern in path by keeping directory structure intact?

I want to copy all files from server A to server B that have the same parent directory-name in different levels of filesystem hierarchy, eg: 我想将所有文件从服务器A复制到服务器B,这些文件在不同级别的文件系统层次结构中具有相同的父目录名,例如:

/var/lib/data/sub1/sub2/commonname/filetobecopied.foo
/var/lib/data/sub1/sub3/commonname/filetobecopied.foo
/var/lib/data/sub2/sub4/commonname/anotherfiletobecopied.foo
/var/lib/data/sub3/sub4/differentname/fileNOTtobecopied.foo

I want to copy the first three files that all have the commonname in path to server B. I already spent a lot of time in finding the correct include/exclude patterns for rsync but I dont get it. 我想前三个文件都具有复制commonname路径服务器B.我已经找到了正确的包含/排除的模式花费了大量的时间rsync ,但我不明白这一点。 The following command does not work: 以下命令不起作用:

rsync -a --include='**/commonname/*.foo' --exclude='*' root@1.2.3.4:/var/lib/data /var/lib/data

I either match too much or to few of the files. 我要么匹配太多,要么与少数文件匹配。 How can I sync only the files with the commonname in its path? 如何仅在路径中同步具有commonname的文件?

I guess you're looking for this: 我猜你在找这个:

rsync -a -m --include='**/commonname/*.foo' --include='*/' --exclude='*' root@1.2.3.4:/var/lib/data /var/lib/data

There are 2 differences with your command: 您的命令有2个不同之处:

  • The most important one is --include='*/' . 最重要的是--include='*/' Without this, as you specified --exclude='*' , rsync will never enter the subdirectories, since everything is excluded. 如果没有这个,正如您指定的--exclude='*'rsync将永远不会进入子目录,因为所有内容都被排除在外。 With --include='*/' , the subdirectories are not excluded anymore, so rsync can happily recurse. 使用--include='*/' ,子目录不再被排除,因此rsync可以愉快地递归。
  • The least important one is -m : this prunes the empty directories. 最不重要的是-m :这会修剪空目录。 Without this, you'd also get the (empty) subdirectory /var/lib/data/sub3/sub4/differentname/ copied. 如果没有这个,你也会得到(空)子目录/var/lib/data/sub3/sub4/differentname/ copied。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM