This script will inspect a directory of your choosing, and for each file in the directory, calculate the SHA-1 hash. A JSON file will then be created called "manifest.json" which lists each file along with its corresponding hash.
Usage: (Created and tested on a Mac)
From a Terminal prompt, enter:
bash manifestMaker.sh dirwhere dir is the directory for which you want to create a manifest.
Script
Download the script from ManifestMaker, or see below for the source code. Remember to make the script executable.
#!/bin/bash
#script which generates a JSON file, which has as keys the filenames of all files in the given directory, and as values the SHA1 digest of the corresponding files
#check number of arguments is as expected
if [ $# -ne 1 ]
then
echo "Need to provide directory on which to operate"
exit 1
fi
src="$1"
#check directory exists
if [ ! -d $src ]
then
echo "Directory $src does not exist"
exit 2
fi
#change to specified directory
cd $src
#declare target output file
out="manifest.json"
#clean up first
rm $out
touch $out
#find the number of files - needed to know whether to put a "," at the end of each line or not
numFiles=$(ls | wc -l)
#start printing to the file
echo "{" >> $out
index=1
for file in *
do
#check if comma is needed at the end of the line
if [ "$index" -lt "$numFiles" ]
then
endLine=","
else
endLine=""
fi
#gives some unfriendly output formatting
digestFull=$(openssl sha1 $file)
#tidy up formatting to get actual value
digest=${digestFull#*= }
#print line to file
echo " \"$file\" : \"$digest\"$endLine" >> $out
#increment our index
index=$[$index+1]
done
echo "}" >> $out
This post was really helpful as i was also in need of smiliar script and you saved the time.
ReplyDeleteThanks,
Sam