I have written a small script to calculate sha1 sums recursively through a directory tree, whoch works here on my system. However, my knowldege of awk scripting is extremely limited, so I'm not sure if it will work anywhere else. I would appreciate anyone testing it who has the ability and the inclination to do so.

#!/bin/bash

do_directory () {
curdir=$1
startdir=$2
for file in *; do
if [[ -f "$file" ]]; then
if [[ -n $curdir ]]; then
sha1sum "$file" | awk -F " " -v directory="$curdir" '{
print $1 " " directory "/" $2
}' >> "$startdir"/sha1sums.txt
else
sha1sum "$file" | awk -F " " -v directory="$curdir" '{
print $1 " " directory $2
}' >> "$startdir"/sha1sums.txt
fi
elif [[ -d "$file" ]]; then
local previousdirectory="$curdir"
if [[ -n $curdir ]]; then
curdir="$curdir/$file"
else
curdir="$file"
fi
cd "$file"
do_directory "$curdir" "$startdir"
curdir="$previousdirectory"

cd ..
fi
done
}

startdir=`pwd`
do_directory "" "$startdir"
The generated hash file should be compatible with the "sha1sum -c" command (at least that's the point). It cannot at this point handle filenames with double spaces in them. This script should be useful for quickly calculating these hashes on files found during forensic investigations, and the results can be piped through netcat.

Any help in testing this would be appreciated.


<edit>
In the two awk statements, the field delimiter specified by -F " " should contain TWO spaces between the quotes, but the PHP engine on this site only displays one. This is necessary to prcess filenames with spaces.
</edit>