Generate MD5 hash of all files under the provided path and all its subdirectories

By xngo on June 23, 2019

#!/bin/bash
 
# Description: Generate MD5 hash of all files under the provided path and all its subdirectories.
# Author: Xuan Ngo
# Usage: thisScriptName.sh [path]
#        thisScriptName.sh .
#        thisScriptName.sh /some/absolute/path
#        thisScriptName.sh some/relative/path
# Output: YYYY-MM-DD HH:MM:SS | MD5 hash | Size in bytes | Filename
#############################################################################################
 
ProcessingPath="$1"
 
find $ProcessingPath -type f | while read filename ; do
 
  lFileSize=`du -sb "$filename" | gawk '{print $1}'`
  # Output: YYYY-MM-DD HH:MM:SS | MD5 hash | Size in bytes | Filename
  md5sum "$filename" | gawk -v hFileName="$filename" -v filesize="$lFileSize" '{print strftime("%Y-%m-%e %H:%M:%S"),"|", $1, "|", filesize, "|", hFileName}'
done

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.