0

I want to include all top level files (file1.txt, file2) and only anything under /top/dir1/. How can I achieve it?

I have tried the following but they don't work

$ tree
.
└── from
    ├── file1.txt
    ├── file2
    └── top
        ├── dir1
        │   └── file3.txt
        └── dir2
            └── file4.txt

one attempt, missing top level files

$ rsync --dry-run \
>       --include='top/' \
>       --include='top/dir1/' \
>       --include='top/dir1/***' \
>       --exclude='top/*' \
>       --exclude="*" \
>       -av from/* .
building file list ... done
top/
top/dir1/
top/dir1/file3.txt

other attempt to include top level files but it's not excluding dir2

$ rsync --dry-run \
>       --include="*" \
>       --include='top/' \
>       --include='top/dir1/' \
>       --include='top/dir1/***' \
>       --exclude='top/*' \
>       --exclude="*" \
>       -av from/* .
building file list ... done
file1.txt
file2
top/
top/dir1/
top/dir1/file3.txt
top/dir2/
top/dir2/file4.txt

1 Answer 1

2

Never use * in the source path for rsync, rsync is perfectly capable of finding those entries itself.

By doing that, you've effectively called rsync as

rsync --dry-run \
   --include="*" \
   --include='top/' \
   --include='top/dir1/' \
   --include='top/dir1/***' \
   --exclude='top/*' \
   --exclude="*" \
   -av from/file1.txt from/file2 from/top .

Include and exclude patterns are relative to the source root; so the --include 'top/' etc. patterns will never match.

Do this:

rsync --dry-run \
   --include="/*" \
   --include='/top/' \
   --include='/top/dir1/' \
   --include='/top/dir1/***' \
   --exclude='/top/*' \
   --exclude="*" \
   -av from/ .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .