Skip to content

Instantly share code, notes, and snippets.

@ianmustafa
Last active August 25, 2018 05:10
Show Gist options
  • Save ianmustafa/175b3b8e5c2da2134fb1d7ae6abb3ea2 to your computer and use it in GitHub Desktop.
Save ianmustafa/175b3b8e5c2da2134fb1d7ae6abb3ea2 to your computer and use it in GitHub Desktop.
md5pv: md5sum with pv and output as file

md5pv: md5sum with pv and output as file

Add progress bar to your md5sum, and save the output as file!

Requirements

md5sum and pv must be installed on the target machine.

Installation

Just download the script and put it into your $PATH and make it executable.

Usage

$ md5pv [-w] filename

Flags

-h: The help message
-w: Write hash output to a .md5 file next to the checked file

References

#!/bin/bash
# md5pv: md5sum with pv and output as file
# Define usage function
usage(){
echo -e "$0: md5sum with pv and output as file\nUsage: $0 [-w] filename"
echo -e "Flags:\n -h\tThis message\n -w\tWrite hash output to a .md5 file next to the checked file"
exit 1
}
# Define is_file_exists function
# $f -> store argument passed to the script
is_file_exists() {
local f="$1"
[[ -f "$f" ]] && return 0 || return 1
}
# Invoke usage
# call usage() function if filename not supplied
[[ $# -eq 0 ]] && usage
# Flag detection
write=false
while getopts :hw opt; do
case $opt in
h) usage; exit ;;
w) write=true ;;
\?) echo "$0: Unknown option -$OPTARG"; exit 1 ;;
esac
done
shift $(( OPTIND - 1 ))
# Invoke is_file_exists
if ( is_file_exists "$1" )
then
output=$(pv $1 | md5sum | awk "{print \$1}")
if ( write )
then
fname=$(echo $1 | awk -F "/" "{print \$NF}")
echo "$output $fname" > "$1.md5"
fi
echo "$output $1"
else
echo "$0: File not found"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment