73

I'm using rsync to recursively sync a remote folder tree that looks something like this:

/folderA/a1/cache
/folderA/a1/cache/A1
/folderA/a1/cache/A2
/folderA/a1/somefolder
/folderA/a1/someotherfolder
/folderA/a2/somefolder/cache
/folderB/cache/
/folderB/b1/somefolder/cache
/folderB/b1/somefolder/yetanotherfolder/cache
/folderB/b1/somefolder/yetanotherfolder/cache/B1
/folderB/b1/somefolder/yetanotherfolder/cache/B2

I don't know what the folder tree will look like and it will change over time. So what I want to be able to do is recursively rsync the above but exclude the folder "cache" and any sub folders it contains:

/folderA/a1
/folderA/a1/somefolder
/folderA/a1/someotherfolder
/folderA/a2/somefolder
/folderB/
/folderB/b1/somefolder
/folderB/b1/somefolder/yetanotherfolder/

Any suggestions?

TheEdge
  • 833

1 Answers1

110

You want the --exclude flag. For example, a local rsync:

rsync -a --exclude cache/ src_folder/ target_folder/

It really is that simple -- that exclude rule will match a directory named cache anywhere in your tree. The / at the end forces it to match directories, so it won't exclude a file named cache.

You can match on paths as well. Given the following tree:

a1/cache/
b1/cache/
sub/a1/cache/

--exclude a1/cache/ will match a1/cache and sub/a1/cache but not b1/cache. And --exclude /a1/cache/ will match only a1/cache.

There's a lot more that can be done with rsync filtering. Look for --exclude, --include, --filter, and especially the "FILTER RULES" and "PATTERN MATCHING RULES" sections on the rsync man page:

https://www.samba.org/ftp/rsync/rsync.html#FILTER_RULES

Jander
  • 16,682