0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.