Bash Script To Check md5sum

Shell Scripting

Below script can be used as a bash script check md5sum. Useful when you have a large number of files to check on daily basis.

#!/bin/bash
#Checks files integrity via md5sum utility.
#For removal of all *.md5 files, execute: find `pwd` -name "*.md5" -exec rm {} \;
#Usage:
#       checksum.sh "/path/to/directory"
#script checksum.log
rm -rf `pwd`/checksum/
rm -rf `pwd`/checksum.tar.gz
mkdir `pwd`/checksum/
echo "$1";
find $1 -name "*.md5" -exec rm {} \;
DATE=`date +%Y.%m.%d_%H.%M.%S.%N`
LOG=`pwd`/checksum/`basename $0`-$DATE.log
touch $LOG

if test ! -w "$LOG"; then
LOG=/dev/stdout
fi

SHIFT=$[`tput cols`-10]
MOVE="\\033["$SHIFT"G"
DEFAULT="\\033[0;39m"
RED="\\033[1;31m"
GREEN="\\033[1;32m"
YELLOW="\\033[1;33m"
BLUE="\\033[1;34m"

logError_end() {
echo -e "$MOVE$RED$1$DEFAULT"
echo -e \\t$1 >>$LOG
}

logOk_end() {
echo -e "$MOVE$GREEN$1$DEFAULT"
echo -e \\t$1 >>$LOG
}

logWarning_end() {
echo -e "$MOVE$YELLOW$1$DEFAULT"
echo -e \\t$1 >>$LOG
}

checkSum() {
#$1=file
echo -en CheckSum:\\t$1
echo -en CheckSum:\\t$1 >>$LOG
file=`basename $1`
dir=`dirname $1`
(cd $dir && md5sum -c -- $file >/dev/null 2>&1 && logOk_end OK || logError_end ERROR)
}

calcSum() {
#$1=file
echo -en CalcSum:\\t$1
echo -en CalcSum:\\t$1 >>$LOG
file=`basename $1`
dir=`dirname $1`
(cd $dir && md5sum -b -- $file >$file.md5 2>/dev/null && logWarning_end CALCULATED || logError_end ERROR)
}

fin_process() {
while read; do
#echo $REPLY
if test -n "`echo $REPLY | grep '\.md5$'`"; then
checkSum $REPLY
else
if test ! -f "$REPLY.md5"; then
calcSum $REPLY
fi
fi
done
}

find $1 -type f | fin_process;
echo
echo -e "\\033[1;31m"
cat $LOG |grep ERROR
echo -e "\\033[0;39m"

echo "Log file locations are:"
echo $LOG
##cp -rf "root/`basename $0`-$DATE.log" `pwd`
echo "collecting *.md5 files ..."
find $1 -name "*.md5" -exec mv -f {} `pwd`/checksum/ \;
echo "Please wait whiile making a tar file....."
tar cvf checksum.tar `pwd`/checksum/
echo zipping checksum.tar
gzip checksum.tar
rm -rf `pwd`/checksum/
exit;

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.