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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0