Search code examples
bashbackuprsync

rsync: Exclude specific filetype in only one directory


I backup my data with rsync and would like to exclude a specific filetype in only one directory (and its subdirectories). For example I have:

$ ls Source/
Folder1/a.tar
Folder1/b.dat
Folder2/c.tar
Folder2/d.dat
Folder2/Subfolder3/e.tar
Folder2/Subfolder3/f.dat
Folder2/Subfolder3/g.pdf

Now I would like to sync all files except for the .tar files in Folder2 and its subfolder. At the end it should look like this:

$ ls Target/
Folder1/a.tar
Folder1/b.dat
Folder2/d.dat
Folder2/Subfolder3/f.dat
Folder2/Subfolder3/g.pdf

Does someone know how to do that? I played around with the --exclude option, but without luck.


Solution

  • rsync manual says

    INCLUDE/EXCLUDE PATTERN RULES
    ...
           o      use ’**’ to match anything, including slashes.
    

    so you can do

    rsync -a --exclude='Folder2/**.tar' Source/ Target
    

    Note this is different from bash's globstar option where you would use Folder/**/*.tar.