Bash diff 在找到第一个差异时停止

Bash diff that stops when it finds the first difference

我有这个用于备份的脚本。问题是它有点慢。我想知道是否有 diff 命令在找到第一个差异时停止。

DocumentsFiles=("Books" "Comics" "Distros" "Emulators" "Facturas" "Facultad" "Laboral" "Mods" "Music" "Paintings" "Projects" "Scripts" "Tesis" "Torrents" "Utilities")
OriginDocumentsFile="E:\Documents\"
DestinationDocumentsFile="F:\Files\Documents\"

## loop file to file and copy in backup
for directory in "${DocumentsFiles[@]}"
do
    RealOrigin="${OriginDocumentsFile}${directory}"
    RealDestination="${DestinationDocumentsFile}${directory}"

    echo $directory
    if [ -a "$RealDestination" ]; then
        echo ok
        if diff -r $RealOrigin $RealDestination; then
            echo "${directory} are equal!"
        else
            rm -rfv $RealDestination
            cp -ruv $RealOrigin "${DestinationDocumentsFile}"
        fi
    else 
        cp -ruv $RealOrigin "${DestinationDocumentsFile}"
    fi
done

diff -q 报告 "only when files differ"(根据 man diff),所以我相信它会在第一次差异后停止。

但这有点XY problem。你真的需要一个更好的备份程序,比如 rsync:

It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination.

来自man rsync