Using a shell script to check if rsync made any changes

5,930

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
Share:
5,930

Related videos on Youtube

Alok Ranjan
Author by

Alok Ranjan

Updated on September 17, 2022

Comments

  • Alok Ranjan
    Alok Ranjan almost 2 years

    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?

  • Alok Ranjan
    Alok Ranjan almost 14 years
    That seems to do exactly what I want... thanks. Didn't think of using the -i flag like that.
  • hamx0r
    hamx0r over 9 years
    To clarify, if you want to run the script only if a change was made, then == should be !=.
  • Admin
    Admin over 8 years
    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.
  • Dor
    Dor over 7 years
    With the -i flag - rsync version 3.1.1 outputs sending incremental file list when no changes are made.