3

The following pattern copies the .pdf files from source to destination, working fine.

rsync -rv --include="*.pdf" --include="*/" --exclude="*" --prune-empty-dirs /source/ ~/destination/

but I want to exclude the files which contains web in file name is not working it is even copying the file web_profile.pdf file to the destination, why it is so.

rsync -rv --include="*.pdf" --include="*/" --exclude="*web*.pdf" --exclude="*" --prune-empty-dirs /source/ ~/destination/

Also I'm not clear about the excluding particular directory like **/ (mean 2 stars and single star).

Can someone please clear my doubts regarding pattern matching in rsync.

--include="*.pdf" matches *pdf, if I want to exclude some directory like ../web-info/..pdf, how do I do it?

Curious
  • 153
  • 2
  • 6

1 Answers1

6

The order of --include and --exclude options is relevant.

You are first including all *.pdf files, the later exclusion of *web*.pdf never applies because of this. Note also that include/exclude patterns apply to node names (files, directories, etc.) and not to pathnames, unless you have a / or ** in the pattern; so excluding *web*.pdf wouldn't exclude ...web.../...pdf anyway.

If you want all PDFs but not those under directories matching *web* then this should work:

rsync -rv --exclude "*web*/" --include="*.pdf" --include="*/" --exclude="*" --prune-empty-dirs /source/ ~/destination/

This will prevent rsync recursing into directories matching *web*, include all *.pdf files, include all other directories, exclude all other files.

Using a ** in the pattern might also work, although I prefer the above solution:

rsync -rv --exclude "**web*.pdf" --include="*.pdf" --include="*/" --exclude="*" --prune-empty-dirs /source/ ~/destination/
wurtel
  • 16,115