Search code examples
rsync

Rsync include, exclude and filters


I am trying to script rsync to quickly copy files in one directory on OSx. I am having an issue with the include and exclude options. For example the directory I would like to copy is:

$HOME/Library/Dir1/RandomDir/cache/

I was initially using rsync to just sync the cache directory, however OSx was not using the wildcard in the source path. The command i was using was:

rsync -avP "$HOME/Library/Dir1/*/cache/" Dest/Dir

When I would run this command OSx was unable to find the directory because it was looking for the literal interpenetration of Dir1/*/ and the * was not being treated as a wildcard.

I then next tried to implement the --include and --exclude options however I have been unable to get this to work.

I was using

rsync -avP --include="*/" --include="cache/" --exclude"*" $HOME/Library/Dir1/ Dest/Dir/

When I run this command it does not copy the cache directory only. It is my understanding that rsync implements the includes and excludes in the order that they are entered, so my logic is that rsync will include everything under the Dir1 directory (taking care of the wildcard issue), then only include the cache directory, then it would exclude everything else. I want to use rsync for its archiving capabilities of keeping file attributes and timestamps across the copy.

Can anyone tell me where I'm going wrong here? I've tried several variants of the command for example --include="/cache" and --exclude="/*", but obviously none have worked so far. Any help at all is greatly appreciated. Thank you.


Solution

  • I finally found where my issue was. I had the source dir in double quotes thus interpreting the * as a literal... Thanks anyway.