2

at the moment I am struggling to find a solution to include only files in a subdirectory but exclude all other stuff. File structure looks like this

$ tree ../
../
├── from
│   ├── a
│   │   ├── aa
│   │   │   └── x.txt
│   │   ├── a.txt
│   │   └── b.txt
│   └── b
│       ├── b.txt
│       └── c.txt
└── to

and only the stuff in a/aa/ shall be transferred into to/.

$ rsync -avh --include=a/aa/*** --exclude=* ../from/ ../to/

does not work and to/ stays empty but doing this with a/

$ rsync -avh --include=a/*** --exclude=* ../from/ ../to/

it works

$ tree ../
..
├── from
│   ├── a
│   │   ├── aa
│   │   │   └── x.txt
│   │   ├── a.txt
│   │   └── b.txt
│   └── b
│       ├── b.txt
│       └── c.txt
└── to
    └── a
        ├── aa
        │   └── x.txt
        ├── a.txt
        └── b.txt

but there a.txt and b.txt should not be there. What did I miss?

Thanks for your help.

New contributor
mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • There might be a way to do that with --include and --exclude; I'm not sure. But wouldn't mkdir -p ../to/aa/a && rsync -avh ../from/aa/a/ ../to/aa/a/ be easier?
    – frabjous
    May 14 at 19:00

1 Answer 1

1

In your first command, a/aa/*** would match aa/ and anything after that, but there is no match for a/, so nothing is copied.

In the second command, a/*** matches a/ and anything after that, so you copy more than just a/aa/ contents.

For example, this should be enough:

rsync -avh --include='/a/' --include='/a/aa/***' --exclude='*' from/ to/

man rsync to INCLUDE/EXCLUDE PATTERN RULES

0

Your Answer

mike is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.