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)

Amelio Vazquez-Reina
  • 40,169
  • 77
  • 197
  • 294
  • Is [Rsync filter: copying one pattern only](http://unix.stackexchange.com/questions/2161/2503) the same problem? If not, please update your question to explain the difference (I'm not sure exactly what your selection criteria are). – Gilles 'SO- stop being evil' Mar 22 '12 at 23:58
  • Thanks @Guilles. From what I understand, I think `rsync` filters are a great fit for this. I went through your answer and read about them a bit more online, but I am not sure how what would be the right rule here. I'll clarify my question too. – Amelio Vazquez-Reina Mar 23 '12 at 13:51
  • I have updated my answer @Gilles. I hope what I am asking for is much clearer now. – Amelio Vazquez-Reina Mar 23 '12 at 14:00

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,252
  • 19
  • 19