0

I have two directories in /test:

$ ls /test/
dir1  dir2

I run:

$ rsync --recursive --links --perms --executability --times --delete /dir2 /test/

My expectation is that /test/dir1 gets deleted, but it doesn't. Is there any way to make that happen? None of the --delete* options of rsync seem to do the job.

Thanks and best regards, Martin

2
  • Logical : rsync a b/ is a short way to type rsync a b/a then b/* excepted b/a is ignored. If you want test to be cleaned, you should prepare a source directory which contains only dir2 and use rsync -r --delete source test without the / after test. Commented Oct 11, 2021 at 8:54
  • @FrédéricLoyer The trailing / on the destination is irrelevant. It's on the source path it's relevant.
    – Kusalananda
    Commented Oct 11, 2021 at 9:30

1 Answer 1

2

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 /).

You must log in to answer this question.

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