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
Related videos on Youtube
Author by
Alok Ranjan
Updated on September 17, 2022Comments
-
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 almost 14 yearsThat seems to do exactly what I want... thanks. Didn't think of using the -i flag like that.
-
hamx0r over 9 yearsTo clarify, if you want to run the script only if a change was made, then
==
should be!=
. -
Admin over 8 yearsWell 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 over 7 yearsWith the
-i
flag - rsync version3.1.1
outputssending incremental file list
when no changes are made.