At: ashok.videdot.com/2008/sfvcheck-script

SFV Check script

Here's a handy little script for checking Simple File Verification (SFV) files. These are commonly used when distributing files over Usenet, where a large file is sliced into many parts and posted separately. It is just a list of checksums of the smaller parts of the file, so doesn't guard against evil-doers, just random errors.

I used to use a GUI program on the Mac, but it started misbehaving and I'm much happier at the command line.


Copy this into a file on your path and chmod a+x it.

 #!/bin/bash                                                                                                
 # By Ashok Argent-Katwala <http://ashok.videdot.com/>. No rights reserved, do with it as you please.       
 
 if [ `sed --version >/dev/null 2>/dev/null && echo 1` ]; then
     sed='sed -r' # GNU sed (e.g. on Linux)                                                                 
 else
     sed='sed -E' # BSD sed (e.g. on Mac OS X)                                                              
 fi
 
 if [ -z "$1" ]; then
     echo 1>&2 "Usage: $0 sfvfile"
     exit
 fi
 
 if [ ! -f "$1" ]; then
     echo 1>&2 "No file found."
     exit
 fi
 
 tab=$'\t'
 sfvfile="$1"
 
 cd "$(dirname "$sfvfile")"
 cat "$(basename "$sfvfile")"| grep -v '^\s*;' | while read line; do
     crc=`echo $line | $sed -e 's/.*[[:space:]]+([0-9A-Fa-f]{8})[[:space:]]*$/\\1/'`
     file=`echo $line | $sed -e 's/(.*)[[:space:]]+[0-9A-Fa-f]{8}[[:space:]]*$/\\1/'`
     if [ ! -f "$file" ]; then
       echo "MISSING$tab$file"
     elif [ "$crc" != `cksum -o 3 "$file" | cut -f 1 -d' ' | $sed -e 's:^:obase=16;:' | bc` ]; then
       echo "BAD$tab$file"
     fi
 done

It could be more efficient, but it gets the job done fairly reliably. It should be fine on GNU/Linux, Mac OS X and anything that has a reasonable version of bash, sed and cksum.

Tagged: Code, Tips

Posted at 11:51 GMT, 26th March 2008.

Last changed at 16:02 GMT, 26th March 2008.

No comments. Add one.