Skip to content

Instantly share code, notes, and snippets.

@KLicheR
Created April 10, 2017 20:50
Show Gist options
  • Save KLicheR/bee6def625a8def1630deda2b4a3408c to your computer and use it in GitHub Desktop.
Save KLicheR/bee6def625a8def1630deda2b4a3408c to your computer and use it in GitHub Desktop.
Generate a SHA256 checksum map of a directory and allow to compare it with another directory
#!/bin/bash
version="1.0.0"
print_help() {
echo "Usage: csmap [dir]"
echo ""
echo "This command generated a \"csmap.txt\" file containing a SHA256 checksum of"
echo "each file of a directory recursively. If a \"csmap.txt\" file exists,"
echo "it generates a temporary \"csmap.compare.txt\" file, do the mapping and"
echo "compare it with the \"csmap.txt\" file."
}
if [ -z "$1" ];then
echo "Error: [dir] has to be specified"
echo ""
print_help
exit 1
fi
if [ "$1" == "-v" -o "$1" == "--version" ];then
echo $version
exit 0
fi
if [ "$1" == "-h" -o "$1" == "--help" ];then
print_help
exit 0
fi
if [ ! -d "$1" ];then
echo "Error: Invalid [dir]"
echo ""
print_help
exit 1
else
directory=$(dirname "$1")
fi
recurse() {
for i in "$1"/*;do
if [ -d "$i" ];then
recurse "$i"
elif [ -f "$i" ]; then
shasum -a 256 "$i" >> "$map_file"
fi
done
}
if [ -f "$directory/csmap.txt" ];then
compare_at_the_end=true
if [ -f "$directory/csmap.compare.txt" ];then
rm "$directory/csmap.compare.txt"
fi
map_file="$directory/csmap.compare.txt"
else
compare_at_the_end=false
map_file="$directory/csmap.txt"
fi
recurse $directory
if [ "$compare_at_the_end" = true ];then
echo "Compare Maps: if there's diffs, they'll appear here..."
# Ignore the "csmap.txt" of the "csmap.compare.txt" file that wasn't in the "csmap.txt" file.
cat "$directory/csmap.compare.txt" | sed '/csmap.txt/d' | diff "$directory/csmap.txt" -
rm "$directory/csmap.compare.txt"
else
echo "Map generated"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment