P.R.
P.R.

Reputation: 3917

rsync copy only specific files from specific subfolders, without creating empty folders

Imagine a file structure like this:

test
├── a
│   ├── 0
│   │   ├── file0.rtf
│   │   ├── file0.txt
│   │   ├── file1.rtf
│   │   ├── file1.txt
│   │   ├── file2.rtf
│   │   └── file2.txt
│   ├── 1
│   │   ├── file0.rtf
│   │   ├── file0.txt
│   │   ├── file1.rtf
│   │   ├── file1.txt
│   │   ├── file2.rtf
│   │   └── file2.txt
│   ├── 2
│   │   ├── file0.rtf
│   │   ├── file0.txt
│   │   ├── file1.rtf
│   │   ├── file1.txt
│   │   ├── file2.rtf
│   │   └── file2.txt
│   └── 3
│       ├── file0.rtf
│       ├── file0.txt
│       ├── file1.rtf
│       ├── file1.txt
│       ├── file2.rtf
│       └── file2.txt
└── b
    ├── 0
    │   ├── file0.rtf
    │   ├── file0.txt
    │   ├── file1.rtf
    │   ├── file1.txt
    │   ├── file2.rtf
    │   └── file2.txt
    ├── 1
    │   ├── file0.rtf
    │   ├── file0.txt
    │   ├── file1.rtf
    │   ├── file1.txt
    │   ├── file2.rtf
    │   └── file2.txt
    ├── 2
    │   ├── file0.rtf
    │   ├── file0.txt
    │   ├── file1.rtf
    │   ├── file1.txt
    │   ├── file2.rtf
    │   └── file2.txt
    └── 3
        ├── file0.rtf
        ├── file0.txt
        ├── file1.rtf
        ├── file1.txt
        ├── file2.rtf
        └── file2.txt

Where I do not know the real names of a and b. Nevertheless, I want to copy all *.txt files from subfolders of a and b (however they are named) if they are in the subfolder 0.

It kind of works if I am using

rsync -avz --include=*/ --include='*/0/*.txt' --exclude=* test/ test2/

However, rsync creates empty folders 1, 2, 3, which I would like to avoid. How do I do this?

Output from the rsync command above:

sending incremental file list
a/
a/0/
a/0/file0.txt
a/0/file1.txt
a/0/file2.txt
a/1/
a/2/
a/3/
b/
b/0/
b/0/file0.txt
b/0/file1.txt
b/0/file2.txt
b/1/
b/2/
b/3/

I want

sending incremental file list
a/
a/0/
a/0/file0.txt
a/0/file1.txt
a/0/file2.txt
b/
b/0/
b/0/file0.txt
b/0/file1.txt
b/0/file2.txt

Upvotes: 0

Views: 3182

Answers (1)

P.R.
P.R.

Reputation: 3917

It turns out the answer is simple. Just adding the --prune-empty-dirs flag does the trick.

rsync -avz --include=*/ --include='*/0/*.txt' --exclude=* --prune-empty-dirs test1/ test2/

That is what I wanted.

Upvotes: 2

Related Questions