10

I've got a bash script that rsync's two directories. Sometimes there's a change, odds are there's nothing.

I want to run a command only if rsync actually made a change (add/update a file). Otherwise I want to just skip it. Is there a return response I should be looking at?

raccettura
  • 141
  • 2
  • 7

1 Answers1

12

If you use the -i option (and don't use the -v option), rsync will only print lines to STDOUT for any changes that were made. Depending on your script, this could look like

if [ -n "$(rsync -i /dir1 /dir2)" ]; then
  run_command;
fi
fukawi2
  • 5,327
  • 3
  • 30
  • 51
Greg D'Arcy
  • 236
  • 2
  • 3
  • That seems to do exactly what I want... thanks. Didn't think of using the -i flag like that. – raccettura Jun 30 '10 at 16:51
  • To clarify, if you want to run the script _only_ if a change was made, then `==` should be `!=`. – hamx0r Mar 03 '15 at 00:18
  • Well as it is, this code is wrong from what I have seen. As for that code you get it Rsync ran FINE or HAS ERROR running, but this code doesn't check if there was a change. I don´t know if other version of Rsync could that, but I´m using RSYNC in Raspbmc (an old implementation we have) and OSMC. This is sad as RSYNC seems to be a wonderful tool. –  Nov 09 '15 at 15:38
  • 3
    With the `-i` flag - rsync version `3.1.1` outputs `sending incremental file list` when no changes are made. – Dor Mar 19 '17 at 16:55