Skip to content

Instantly share code, notes, and snippets.

@samredai
Created February 28, 2020 21:40
Show Gist options
  • Save samredai/4bef4da50715c04007d2b8f5bf7fc2a0 to your computer and use it in GitHub Desktop.
Save samredai/4bef4da50715c04007d2b8f5bf7fc2a0 to your computer and use it in GitHub Desktop.
Linux: Check if the MD5 hashes are the same between two files
#!/usr/bin/env bash
# Usage: ./md5sum.sh file1 file2
if [ "$#" -ne 2 ]; then
echo -e "Too many arguments,\n\nUsage:\n$ ./check.sh file1 file2"
fi
validate_files() {
status=0
for file in "$@"
do
# Do any preliminary checks of the file here
if [ ! -f $file ]; then
echo -e "\033[1;31mERROR: $file does not exist!" && status=1
fi
done
if [ $status == 1 ]; then exit 1; fi
}
validate_files $1 $2
get_md5_hash() {
echo $(md5sum $1 | awk '{print $1}')
}
compare_md5 () {
file1_hash=$(get_md5_hash $1)
file2_hash=$(get_md5_hash $2)
if [[ $file1_hash == $file2_hash ]]
then
echo -e "\033[1;32mINFO: md5 hash MATCH between $1 and $2"
else
echo -e "\033[1;31mERROR: md5 hash MISMATCH between $1 and $2"
fi
}
compare_md5 $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment