Skip to content

Instantly share code, notes, and snippets.

@kangmasjuqi
Last active August 13, 2018 12:40
Show Gist options
  • Save kangmasjuqi/0d1aa16add249678bec62fc0b00aaec5 to your computer and use it in GitHub Desktop.
Save kangmasjuqi/0d1aa16add249678bec62fc0b00aaec5 to your computer and use it in GitHub Desktop.
generate md5sum keys for all files under targetted folder
#!/bin/bash
##########################################################################################
##########################################################################################
## Bismillaahirrahmaanirrahiim..
## Allahumma sholli 'alaa sayyidinaa Muhammad..
##
## create_md5sum_all_file.sh
## created by marjuqi r.
## august 2018
##
#
# this script generate md5sum keys for all files under targetted folder
# recursively and then stored them on file MD5SUM_FILES_$dir_as_suffix
#
# format :
# ./create_md5sum_all_file.sh [_FULL_PATH_OF_TARGET_FOLDER_|.]
#
# NOTE :
# use dot (.) to choose current directory as a _FULL_PATH_OF_TARGET_FOLDER_
#
# example :
# 1. ./create_md5sum_all_file.sh /home/marjuqi/pdf/
# 2. ./create_md5sum_all_file.sh .
#
# output :
# the MD5SUM file will created on your current working directory
#
##########################################################################################
##########################################################################################
# this function convert "/home/marjuqi/Works/TOOLS/sql/" into "_home_marjuqi_Works_TOOLS_sql_"
replace_slash(){
fullpath=$1
result=${fullpath//\//_}
echo $result
}
if [ $1 = "." ]; then
fullpath=$(pwd)
else
fullpath=$1
fi
dir_as_suffix=$(replace_slash $fullpath)
filename="MD5SUM_FILES_$dir_as_suffix"
echo "======= START ======="
n_files=$(find $fullpath -type f ! -name $filename | wc -l)
echo "======= 1. Finding all files : $n_files found.. ======="
find $fullpath -type f ! -name $filename | xargs -I{} md5sum {} > $(pwd)/$filename
echo "======= 2. Writing to file $filename.. ======="
echo "======= 3. Checking md5sum integrity.. ======="
yes "" | md5sum -c $filename
echo "======= FINISH ======="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment