47

I'm using rsync to sync files from a source to a destination:

rsync -av --delete source destination

I have a single directory on the destination side which is not on the source side. I'd like to prevent rsync from deleting this directory. Is there an option I can pass to rsync to prevent this directory from being deleted upon sync?

turtle
  • 7,533
  • 18
  • 68
  • 97

2 Answers2

54

You can exclude files/directories with --exclude. This will prevent the somedir directory from being synced/deleted:

rsync -avrc --delete --exclude somedir source destination
kielni
  • 4,779
  • 24
  • 21
  • 5
    If you want to exclude `somedir` from only one specific location in the directory tree you can anchor the exclude with a "/". This will improve performance - rsync will only have to check the path once! eg. `rsync -avrc --delete --exclude /path/p2/somedir source destination` – Randy Skretka Feb 26 '14 at 17:47
  • 1
    `-r` is implied by `-a` and not required. – Tom Hale Jan 18 '17 at 02:05
  • 1
    For anyone wondering what the `rc` are about in the above `-avrc`, they have nothing to do with this question. (r=recursive, c=skip based on checksum) – Joschua May 15 '19 at 19:49
51

As mentioned in a similar question, this can be accomplished by using the --filter option with protect rule:

$ rsync ... --filter 'protect /remote-directory-to-keep/' ...

Unlike the currently accepted answer, using --filter is useful, for instance, if you also wish to use --exclude with --delete-excluded.

Community
  • 1
  • 1
asaveljevs
  • 2,220
  • 1
  • 16
  • 13