How to rsync files with matching pattern in path by keeping directory structure intact? How to rsync files with matching pattern in path by keeping directory structure intact? shell shell

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


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.