Since you have no /
at the end of the source path (/dir2
), you are instructing rsync
to sync /dir2
with /test/dir2
. With --delete
added, rsync
would remove all things under /test/dir2
that do not exist in /dir2
.
At no point is /test/dir1
accessed or considered.
However, if you use /dir2/
as the source path, rsync
would sync /dir2
with /test
(not /test/dir2
). Adding --delete
would delete both /test/dir2
and /test/dir1
as neither /dir2/dir1
nor /dir2/dir2
presumably exists.
The trailing /
on the destination path is irrelevant, but the trailing /
on the source path decides whether the source directory should be synced as a separate directory under the destination path (without /
), or whether rsync
should sync it to the destination path itself (with /
).
rsync a b/
is a short way to typersync a b/a
then b/* excepted b/a is ignored. If you wanttest
to be cleaned, you should prepare a source directory which contains onlydir2
and usersync -r --delete source test
without the/
after test./
on the destination is irrelevant. It's on the source path it's relevant.