Linux – test when directories are in sync using rsync

bashlinuxrsync

Is there any way to know from a bash script when a rsync command does not download anything (aka the folder is in sync)?, I've tried using the exit code, but is only different when an error happens.

Example of rsync downloading only the files not in sync:

$ rsync -av –progress humbo@hkremote:/home/humbo/dumps/*.gz .

receiving file list ...
9 files to consider
_dump_7.gz
    35927279 100%    1.19MB/s    0:00:28 (xfer#1, to-check=1/9)
_dump_8.gz
    39387704 100%    1.17MB/s    0:00:32 (xfer#2, to-check=0/9)

sent 64 bytes  received 75324472 bytes  1205192.58 bytes/sec
total size is 354448073  speedup is 4.71

$ echo $?

0

Files in sync:

$ rsync -av –progress humbo@hkremote:/home/humbo/dumps/*.gz .

receiving file list ...
9 files to consider

sent 20 bytes  received 205 bytes  90.00 bytes/sec
total size is 354448073  speedup is 1575324.77

$ echo $?

0

Best Answer

You can use rsync --stats then use grep "Number of files transferred" and then print this stat with awk, in this way:

rsync -av --stats --progress humbo@hkremote:/home/humbo/dumps/*.gz . | grep 'Number of files transferred' | awk -F ": " '{print $2}'

This result is 0 when nothing is transfered.