25

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, e.g:

/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. 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?

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
murks
  • 299
  • 1
  • 3
  • 8
  • 2
    You should probably ask this on the [Unix & Linux](http://unix.stackexchange.com/) stackexchange, or on [ServerFault](http://serverfault.com/). – Stephen P Feb 10 '15 at 19:17

1 Answers1

54

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:

  • The most important one is --include='*/'. Without this, as you specified --exclude='*', rsync will never enter the subdirectories, since everything is excluded. With --include='*/', the subdirectories are not excluded anymore, so rsync can happily recurse.
  • The least important one is -m: this prunes the empty directories. Without this, you'd also get the (empty) subdirectory /var/lib/data/sub3/sub4/differentname/ copied.
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104