How to copy a subset of files from a directory while preserving the folder structure

directory-structurefile-copy

I want to move some subset of files from dirA to dirB (let's say files with "blah" in the filename), but I want all the nested directories to be the same in the new location. How can I do that?

Best Answer

The magic of rsync filter rules:

$ rsync -av --filter="+ */" --filter="-! *blah*" /source /dest

Consult the rsync man page for the details on filter rules, but here's the condensed version for this particular need.

--filter="+ */" means "include everything that is a directory"

--filter="-! *blah* means "exclude everything that does NOT include blah in the filename"

Related Question