Rsync – How to Delete Extraneous Files from Destination Directory

rsync

Say I have

SRC 
  001.jpg
  002.jpg
  001.txt
  a.zip

DEST
  hello.jpg

rsync -d --delete SRC:{*.jpg,*.txt} DEST

It doesn't remove hello.jpg from DEST, any idea how to archive this?

Best Answer

The reason your command isn't working is explained by the manual page for rsync (emphasis added):

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

Thus, when you run

$ rsync -d --delete SRC:{*.jpg,*.txt} DEST

the unwanted files in DEST are not being deleted because you haven't actually asked for a directory to be synced, but just for a handful of specific files. To get the results you desire, try something like this:

rsync -d --delete-excluded --include '*.jpg' --include '*.txt' --exclude '*' SRC/ DEST/

Notice that the order of the include and exclude directives matter. Essentially, each file is checked against the include or exclude patterns in the order that they appear. Thus, files with .jpg or .txt extensions are synced since they match the "included" patterns before they match the excluded "*" pattern. Everything else is excluded by the --exclude '*' pattern. The --delete-excluded option ensures that even excluded files on the DEST side are deleted.

Related Question