Linux Shell Script to cksum and Compare Local and iPod Disks

Submitted by nigel on Sunday 2nd January 2011

After being bedeviled with disk problems on my FAT formatted 5th Generation Video iPod I put together a script to checksum (cksum) the drive and compare against the checksum of the music files on my local laptop. In reality, the this script can be used to check any two drives and it is not mandatory one of them needs to be an iPod drive. All the other examples of this sort of script on the Internet I found had one basic problem - they all only checked one directory, whilst I had a tree structure under my ~/Musicdirectory. In addition, special coding is required for the circumstance where filenames contain spaces - which in fact relates to every single file in my collection.

Ok, the code is reproduced below.

#!/bin/bash
 
# Shell script to compare cksum difference between two directory trees
#
# Copyright badzilla.co.uk
# If this helps you get something working, it would be lovely to receive
# a dollar or two donation to help the badzilla site pay its way
 
ORIGINAL_DIR=~/Music
IPOD_DIR=/media/BADZILLA
 
TEMP_DIR=/tmp
TEMP_FILE1=orig
TEMP_FILE2=target
 
 
 
cd $ORIGINAL_DIR
find . -type f -print0 | while read -d $'\0' FILE
do
    cksum "$FILE" | cut -f1 -d" " > $TEMP_DIR/$TEMP_FILE1
    cksum "$IPOD_DIR/$FILE" | cut -f1 -d" " > $TEMP_DIR/$TEMP_FILE2
 
    diff "$TEMP_DIR/$TEMP_FILE1" "$TEMP_DIR/$TEMP_FILE2" 2>&1 > /dev/null
    if [ $? -ne 0 ]
    then
        echo "Problem with file $IPOD_DIR/$FILE"
    fi
done
 
rm "$TEMP_DIR/$TEMP_FILE1"
rm "$TEMP_DIR/$TEMP_FILE2"
To run it, you will of course need to set the correct permissions:
badzilla@laptop4:~> cd ~/check_ipod
badzilla@laptop4:~/check_ipod> chmod u+x check_ipod
badzilla@laptop4:~/check_ipod> ./check_ipod
After a while, should there be any errors, they will be output to the screen
badzilla@laptop4:~/check_ipod> ./check_ipod
cksum: /media/BADZILLA/./Massive Attack - Heligoland/03 Massive Attack - Heligoland - Splitting The Atom feat. Horace Andy.mp3: Input/output error
Problem with file /media/BADZILLA/./Massive Attack - Heligoland/03 Massive Attack - Heligoland - Splitting The Atom feat. Horace Andy.mp3
cksum: /media/BADZILLA/./Massive Attack - Heligoland/07 Massive Attack - Heligoland - Paradise Circus feat. Hope Sandoval.mp3: Input/output error
Problem with file /media/BADZILLA/./Massive Attack - Heligoland/07 Massive Attack - Heligoland - Paradise Circus feat. Hope Sandoval.mp3
Problem with file /media/BADZILLA/./Massive Attack - Heligoland/10 Massive Attack - Heligoland - Atlas Air.mp3
Problem with file /media/BADZILLA/./Massive Attack - Heligoland/08 Massive Attack - Heligoland - Rush Minute.mp3
By the way, don't expect this script to finish quickly unless you only have a few files to check. Hope this work helps somebody out there!
blog terms
Linux