rsync won't delete files on destination

Solution 1:

Remove the *. As mentioned in the rsync man pages the --delete option doesn't work with wildcard entries.

Use this instead:

rsync -a --delete $DIR1/ $DIR2/

" --delete This tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side), but only for the directories that are being synchronized. You must have asked rsync to send the whole directory (e.g. dir or dir/) without using a wildcard for the directory’s contents (e.g. dir/*) since the wildcard is expanded by the shell and rsync thus gets a request to transfer individual files, not the files' parent directory. Files that are excluded from the transfer are also excluded from being deleted unless you use the --delete-excluded option or mark the rules as only matching on the sending side (see the include/exclude modifiers in the FILTER RULES section). "

Solution 2:

The reason is because you are calling rsync on /tmp/1/b, which will not consider the /tmp/1/a file at all.

Your intention seems to be to rsync the directory /tmp/1/ -- if you use "/tmp/1/" as the source rather than the individual files, it will notice that "a" has been deleted from the directory and remove it from the target.


Solution 3:

If you change the second rsync line to rsync -a --delete $DIR1/ $DIR2 (without the *) it will work. The reason is that the shell expands the * to the file names in the first directory, which is only b in your case, so the missing file a won't be considered at all by rsync, as the command that gets executed is actually rsync -a --delete $DIR1/b $DIR2.

Tags:

Linux

Rsync