Check files for holes

It's quite hard to tell if a file under Linux has holes (eg. it is a sparse file). This script tries to determine this by comparing apparent and real file sizes.

#!/bin/sh
 
where=${1:-.}
 
find "$where" -type f | while read file; do
 
#    apparentsize=`du --apparent-size "$file" | cut -f 1`
#    realsize=`du "$file" | cut -f 1`
 
    blocks=`stat -c "%b" "$file"`
    blocksize=`stat -c "%B" "$file"`
    realsize=$(($blocks * $blocksize))
    apparentsize=`stat -c "%s" "$file"`
 
    diff=$(($apparentsize < $realsize))
 
    if [ $diff -eq 0 ]; then
        echo [has holes?] $file [real:${realsize} apparent:${apparentsize}]
        hole='found'
    fi
 
done
 
if [ -z $hole ]; then
 
    echo "no hole candidates found"
 
fi