How to rsync only files from different subdirs that contain a particular string in the filename

rsync

I have a tree of subdirs, some of which contain a file with the word "template" as part of the name. I want to rsync them to the same subdir tree in a different location.

eg:
Source:

curfolder/dir1/dir2/blah_template.ext
curfolder/dir3/dir4/foo_template.blah

Intended destination:

destfolder/dir1/dir2/blah_template.ext
destfolder/dir3/dir4/foo_template.blah

This command didn't work; it put all the files in the base destfolder, rather than in the correct subdirectories:

rsync -av curfolder/*/*/*template* destfolder

So I think I need a filter something like this, but this isn't working (it seems to just sync everything):

rsync -av -f"+ *template*" curfolder destfolder

Best Answer

One way to do it:

rsync -av --include "*/" --include="*_template*" --exclude="*" --prune-empty-dirs curfolder/ destfolder/  

rsync man

what it does, expressed in words:

  1. include every folder
  2. include everything matching _template
  3. exclude everything else
  4. delete empty folders

The two includes are taking precedence over the exclude, so the exclude only matches everything the includes did not match.