Calamity Jane
Calamity Jane

Reputation: 2686

rsync excluding cache directories

I use rsync as a backup tool to my external hard disk. However I want to save time by no copying useless cache files of several programs. I want to achieve this by omitting every file which is somewhere in the tree under a folder called Cache, cache or cache2 In fact any variations of names which hint to a cache folder in the full path.

They can be in paths like:

.cache/google-chrome/Default/Cache/1e1cb5d5222c54c4_0
.cache/mozilla/firefox/smifthya.default/cache2/entries/15444D7EEEAF61418021BC35F25FD997974458B5

So I try to exclude those cache files from being synched by the following lines so far not successful.

What I tried so far:

rsync -va --delete --exclude={*Cache*, *cache*} /var/www/ $MYEXDISK/www

rsync -va --delete --exclude '*cache*' /var/www/ $MYEXDISK/www

rsync -va --delete --exclude 'cache*' /var/www/ $MYEXDISK/www

rsync -va --delete --exclude={Cache*, cache*} /var/www/ $MYEXDISK/www

Can anybody tell me the corect syntax to exclude any file, in which's path there is a variation of Cache or cache?

Upvotes: 4

Views: 2807

Answers (1)

Marlen T. B.
Marlen T. B.

Reputation: 874

I believe you want:

set -B - brace expansion mode (possibly optional)

rsync -va --delete --exclude={**/Cache*,**/cache*} /var/www/ $MYEXDISK/www

See YoLinux then seach for INCLUDE/EXCLUDE PATTERN RULES. The relevant info is:

use '**' to match anything, including slashes.

Extra info

The {...,...,...} is referred to as brace expansion. It has very specific rules.

  1. Spaces are not allowed.
    • See (good) echo --exclude={foo,bar} => --exclude=foo --exclude=bar
    • vs. (bad) echo --exclude={foo, bar} => --exclude={foo, bar}
  2. A single element expansion will not work.
    • See (bad) echo --exclude={foo} => --exclude={foo}
  3. Sometimes quoting the results helps, sometimes it breaks things. Try both.
    • See (good) echo --exclude=\'{foo,bar}\' => --exclude='foo' --exclude='bar'
  4. If your file names have spaces you need to muli-quote the string
    • See (good) echo --exclude=\'{"foo bar",baz}\' => --exclude='foo bar' --exclude='baz'
    • vs. (bad) echo --exclude={'foo bar',baz} => --exclude=foo bar --exclude=baz
    • vs. (bad) echo --exclude=\'{foo bar,baz}\' => --exclude='{foo bar,baz}'

Upvotes: 5

Related Questions