Skip to content

Instantly share code, notes, and snippets.

@goodjob1114
Last active December 29, 2015 16:29
Show Gist options
  • Save goodjob1114/cef0862800a50dd8b985 to your computer and use it in GitHub Desktop.
Save goodjob1114/cef0862800a50dd8b985 to your computer and use it in GitHub Desktop.
partial md5sum
#!/bin/bash
help_info() {
echo "Usage : $0 file"; exit 0
}
if [ $# -lt 1 ]; then
help_info
else
if [ -f "$1" ]; then
FILE=$1
else
help_info
fi
fi
OS=$(uname -a | awk '{print $1}')
case $OS in
Linux) MD5=md5sum;;
Darwin) MD5=md5;;
*) echo "Your device is not supported yet."; exit 0;;
esac
# BLOCK_SIZE 1M
BLOCK_SIZE=1048576
filesize=$(ls -al "$FILE" | awk '{print $5}')
# stage0 0 ~ 30M, All
if [ $filesize -lt $(expr 30 \* $BLOCK_SIZE) ]; then
dd if="$FILE" bs=$BLOCK_SIZE 2> /dev/null | $MD5 | awk '{print $1}'; exit 0
fi
# stage1 30 ~ 200M, 1/3
# stage2 200 ~ 400M, 60M
# stage3 400 ~ 600M, 80M
# stage* 600M ~ ,100M
threshold_200M=$(expr 200 \* $BLOCK_SIZE)
stage_x=$(expr $filesize / $threshold_200M)
one_second() {
half=$(expr $(expr $filesize / 2) / $BLOCK_SIZE)
}
one_third() {
a_third=$(expr $(expr $filesize / 3) / $BLOCK_SIZE)
}
case $stage_x in
0) one_third; dd if="$FILE" bs=$BLOCK_SIZE skip=$a_third count=$a_third 2> /dev/null | $MD5 | awk '{print $1}';;
1) one_second; dd if="$FILE" bs=$BLOCK_SIZE skip=$(expr $half - 30) count=60 2> /dev/null | $MD5 | awk '{print $1}';;
2) one_second; dd if="$FILE" bs=$BLOCK_SIZE skip=$(expr $half - 40) count=80 2> /dev/null | $MD5 | awk '{print $1}';;
*) one_second; dd if="$FILE" bs=$BLOCK_SIZE skip=$(expr $half - 50) count=100 2> /dev/null | $MD5 | awk '{print $1}';;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment