I recently discovered that a few files on a volume had been mysteriously truncated - or more accurately, replaced with a zero length file.

Fortunately, these files were hard-linked from multiple places, and only about half the links were actually overwritten with a zero-length file with seemingly the same attributes.

However, when I looked in my backups, I found the issue had occurred before my previous backup. For the volume in question, a simple mirror backup should be sufficient, because files shouldn't get deleted - it's mostly append only. But since the file isn't actually being deleted, I can't simply tell rsync to not delete.

Is there a way to tell rsync to not sync a file that is zero length locally, but has a non-zero length on the remote side?

I could of course simply write some code to look for zero length files and exclude them, but this is an extra time-consuming step (now there are two full scans of the filesystem) and it could exclude files which legitimately should be rsynced (in case there are files that are expected to be zero length).

  • You might want to look at rsync's --backup options...
    – derobert
    Apr 1 '15 at 7:48
  • 1
    Also, it's not possible to have some of the hard links to a file be of a different size. All hard linked names are to the same file. If this isn't just a case of using the wrong term, and you actually have two hard links to the same file showing a different size—you have something very, very broken.
    – derobert
    Apr 1 '15 at 7:50
  • @derobert The hard links appears to have been replaced by zero length files which had the same file name and timestamp as the original file.
    – Michael
    Apr 1 '15 at 17:05

rsync has a --min-size option:

 --min-size=SIZE         don't transfer any file smaller than SIZE

That should help you unless you have files that do have to be transferred if there is no zero byte sized file on the destination side of things.

AFAIK there is no way to tell rsync to take special action based on source and destination size.

  • And SIZE is in bytes? kilobytes? troglodytes? (Sorry, looked at the man page but couldn't find it there)
    – Sixtyfive
    Jan 28 '20 at 10:06
  • 1
    If you are to lazy to read the source, you can of course try and see what happens. It is bytes, but you can at least specify M for megabytes.
    – Anthon
    Jan 28 '20 at 14:53
  • 1
    @Sixtyfive it's in the documentation, man rsync. If you look up --min-size you'll see it refers you to --max-size, where SIZE is defined
    – roaima
    Mar 26 at 9:07

NB: to avoid over-writing files with a zero length file, you'd need to set --min-size=1 It's not possible to have a file smaller than zero size. Unless the man page is in error, and it should say "don't transfer any file smaller than or equal to SIZE"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.