#!/bin/sh # wipefile - completely overwrite a file # Originally by Heiner Steven (heiner.steven@odn.de), 7/96 # Expanded by Lorance Stinson . # Last modified 2006-06-11 # To adapt to System V vs. BSD 'echo' if echo '\c' | grep -s c > /dev/null ; then # BSD NOCR1='-n' NOCR2="" else # System V NOCR1="" NOCR2='\c' fi BASENAME=`basename $0` # This programs name. PASSES=1 # The default number of passes to make. RANDEV='/dev/random' # The default random device. #************************************************************************** # usage() - Display the command line help text. #************************************************************************** usage(){ if [ "$1" ] ; then echo "$1" echo "" fi shift cat< The number of passes to make. Default: $PASSES -q Quite mode. No output is generated, assumes -y. -r Remove the file(s) when finished. -u Use /dev/urandom instead of /dev/random. -y Do not request confirmation. EOHELP exit 1 } #************************************************************************** # Parse the command line arguments. #************************************************************************** FILES="" while [ "$#" -gt 0 ] ; do case "$1" in # Print the help text. -h) usage;; # The number of passes to make -p) PASSES="$2"; shift 2;; # Quite. -q) QUIET="Y"; shift;; # Remove the file when finished. -r) REMOVE="Y"; shift;; # Use /dev/urandom. -u) RANDEV='/dev/urandom'; shift;; # Assume yes. -y) ASSYES="Y"; shift;; # Anything after -- is files to process. --) FILES="$FILES $@"; break;; # Check for invalid options. -*) usage "Invalid option $1";; # Anything should be a file. *) FILES="$FILES $1"; shift;; esac done # Restore the files. eval set X "$FILES" shift #************************************************************************** # Wipe the files given on the command line. #************************************************************************** for FILE ; do [ ! -w "$FILE" -a "$QUITE" = "" ] && echo "$BASENAME: $FILE: File not found or is not writable." && continue SIZE=`ls -ld "$FILE" | awk '{print $(NF-4)}'` if [ "$QUITE" = "" -a "$ASSYES" = "" ] ; then echo $NOCR1 "$BASENAME: really overwrite $NOCR2" [ "$REMOVE" = "Y" ] && echo $NOCR1 "and delete $NOCR2" echo $NOCR1 "$FILE ($SIZE bytes) (y/n)? $NOCR2" read RESPONSE || exit 2 case "$RESPONSE" in y*|Y*) ;; *) continue;; esac fi [ "$QUIET" = "" ] && echo "$BASENAME: wiping $FILE for $PASSES passes..." PASS=0 while [ $PASS -lt $PASSES ] ; do [ "$QUIET" = "" ] && echo "$BASENAME: $FILE: Pass `expr "$PASS" + 1`: filling with 0x00" [ `dd if=/dev/zero of="$FILE" bs=1 count="$SIZE" 2>/dev/null` ] && echo "Error wiping $FILE." 1>&2 && continue 2 sync [ "$QUIET" = "" ] && echo "$BASENAME: $FILE: Pass `expr "$PASS" + 1`: filling with 0xFF" [ `yes | tr "y\\n" "\\377" | dd of="$FILE" bs=1 count="$SIZE" 2>/dev/null` ] && echo "Error wiping $FILE." 1>&2 && continue 2 sync [ "$QUIET" = "" ] && echo "$BASENAME: $FILE: Pass `expr "$PASS" + 1`: filling with 0XAA" [ `yes | tr "y\\n" "\\252" | dd of="$FILE" bs=1 count="$SIZE" 2>/dev/null` ] && echo "Error wiping $FILE." 1>&2 && continue 2 sync [ "$QUIET" = "" ] && echo "$BASENAME: $FILE: Pass `expr "$PASS" + 1`: filling with 0x55" [ `yes | tr "y\\n" "\\125" | dd of="$FILE" bs=1 count="$SIZE" 2>/dev/null` ] && echo "Error wiping $FILE." 1>&2 && continue 2 sync [ "$QUIET" = "" ] && echo "$BASENAME: $FILE: Pass `expr "$PASS" + 1`: using $RANDEV" [ `dd if=$RANDEV of="$FILE" bs=1 count="$SIZE" 2>/dev/null` ] && echo "Error wiping $FILE." 1>&2 && continue 2 sync PASS=`expr $PASS + 1` done [ "$REMOVE" = "Y" -a "$QUIET" = "" ] && echo "$BASENAME: deleting $FILE." [ "$REMOVE" = "Y" ] && rm -f "$FILE" done