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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThe 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.
--delete-excluded
. Without it, only *.jpg
and *.txt
may be deleted on the destination side. With it, unrelated files would be deleted as well. You may be interested in this tutorial on rsync filters.
Jan 9, 2011 at 16:09