Skip to content

Instantly share code, notes, and snippets.

@jneen
Created June 26, 2012 00:32
Show Gist options
  • Save jneen/2992325 to your computer and use it in GitHub Desktop.
Save jneen/2992325 to your computer and use it in GitHub Desktop.
#!/bin/bash
usage() {
cat <<EOF
Usage:
$0 [-r|--recursive] <fname>
$0 [-h|-?|--help]
-r --recursive
recursively descend into a directory, fingerprinting all files
-h --help -?
display this message
EOF
}
[[ -z "$CHECKSUM" ]] && CHECKSUM=md5sum
checksum() {
"$CHECKSUM" "$@" | cut -d' ' -f1
}
fingerprint-recursive() {
local dirname="$1"; shift
find "$dirname" -type f | while read fname; do
fingerprint "$fname"
done
}
fingerprint() {
local fname="$1"; shift
local initpwd="$(pwd)"
if [[ ! -s "$fname" ]]; then
echo "$0: $fname does not exist or is not readable. Aborting."
return 1
fi
local basename="$(basename "$fname")"
local dirname="$(dirname "$fname")"
cd "$dirname"
local checksum="$(checksum "$basename")"
local base="${basename%.*}"
local ext="${basename##*.}"
if [[ "$ext" == "$basename" ]]; then
ext=""
else
ext=".$ext"
fi
local fingerprint="$base.$checksum$ext"
local manifest="$basename.manifest"
cp "$basename" "$fingerprint"
echo "$fingerprint" > "$manifest"
echo "$fingerprint"
cd "$initpwd"
}
main() {
local FINGERPRINT=fingerprint
for arg in "$@"; do
case "$arg" in
-r|--recursive) FINGERPRINT=fingerprint-recursive ;;
-h|-?|--help) usage; return 0 ;;
*) "$FINGERPRINT" "$arg" ;;
esac
done
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment