Skip to content

Instantly share code, notes, and snippets.

@paullaffitte
Created August 21, 2021 15:55
Show Gist options
  • Save paullaffitte/23561c9bd3c435891dde9e376abc3be2 to your computer and use it in GitHub Desktop.
Save paullaffitte/23561c9bd3c435891dde9e376abc3be2 to your computer and use it in GitHub Desktop.
Recursively compare files
#! /bin/bash
if [[ $# != 2 ]]; then
echo "usage: cmprec <source1> <source2>"
exit 1
fi
SOURCE1=$1
SOURCE2=$2
echo "Preparing filenames list..." 1>&2
filenames="$((
cd "$SOURCE1"; find -type f; cd - > /dev/null
cd "$SOURCE2"; find -type f; cd - > /dev/null
) | sort | uniq)"
echo "List ready, starting to compare files!" 1>&2
total=$(echo "$filenames" | wc -l)
i=0
echo "$filenames" | while IFS="\n" read -r filename;
do
filename=${filename:2}
if cmp "$SOURCE1/$filename" "$SOURCE2/$filename" --quiet; then
echo "OK $filename"
else
echo "DIFF $filename"
fi
i=$((i+1))
percent=$(echo "scale=2; $i * 100 / $total" | bc -l)
echo -ne "$i/$total files ($percent%)\r" 1>&2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment