0

Say I have the following structure on path origin_path

origin_path/X=1/A/...
origin_path/X=1/B/...
origin_path/X=1/C/...
...
origin_path/X=2/A/...
origin_path/X=2/B/...
origin_path/X=2/C/...
...
...

I would like to replicate the structure above at a different path called destination_path, but only including the paths corresponding to the directories B above.

In other words, I would like to get the following at destination_path

destination_path/X=1/B/...
destination_path/X=2/B/...
destination_path/X=3/B/...
...

To clarify, some of this structure in destionation path might exist already (e.g. the folders destination_path/X=*).

From what I understand, thanks to @Gilles' comment, rsync filters may be a great fit for this. However, I have never used them before and it's a bit hard for me to extrapolate the example provided in Rsync filter: copying one pattern only to my situation.

In what order should I include or exclude things in my case? And how do I tell rsync to use origin_path and destination_path as global paths? (as opposed to having it copy things with relative paths)

1 Answers1

2

The following should do what you want:

rsync --recursive --prune-empty-dirs --include '*/' --include '*/B/**' --exclude '**' origin_path/ destination_path/

The first rule includes all directories (otherwise rsync won't descend into the top level directories). The second rule includes everything in a "B" subdirectories. The third rule excludes everything else. The --prune-empty-dirs option ignores empty directories (since we're including all directories with the first rule).

mgorven
  • 2,292
  • 19
  • 19