51

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?

72
+50

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.

  • @Cheng, care to explain a bit more? I'd be happy to update my answer if there is a problem/improvement. – Steven D Jan 8 '11 at 4:45
  • It works. I guess. The --process things is confusing me, make me thought it was transfered again. – Cheng Jan 9 '11 at 7:23
  • I've edited my answer to explain a bit more. – Steven D Jan 9 '11 at 8:07
  • 2
    @Cheng: I suspect you don't want the --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. – Gilles 'SO- stop being evil' Jan 9 '11 at 16:09
  • 3
    Thanks! For me I was missing the trailing slash on the source directory. – vaughan Oct 11 '12 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.