Skip to content

Instantly share code, notes, and snippets.

@tlrobinson
Created January 23, 2015 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlrobinson/a9551de628a9621af8d2 to your computer and use it in GitHub Desktop.
Save tlrobinson/a9551de628a9621af8d2 to your computer and use it in GitHub Desktop.
OS X tool (easily portable to other *nixes) that writes random data to a disk (e.x. SD card) and reads it back, comparing hashes of the data. Use with caution.
#!/usr/bin/env bash
set -eu
disk_file="$1"
bytes="$(diskutil info "$disk_file" | grep -Eo '\(\d+ Bytes\)' | grep -Eo '\d+')"
block_size="$((1024 * 1024))"
blocks="$((bytes / block_size))"
read -p "Are you sure you want to unmount and overwrite $disk_file? " -n 1 -r; echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
diskutil unmountDisk "$disk_file"
dd bs="1m" count="$blocks" if="/dev/urandom" 2>/dev/null | tee >(md5 1>original.md5) | dd of="$disk_file"
dd bs="1m" count="$blocks" if="$disk_file" | md5 >result.md5
cat original.md5 result.md5
rm original.md5 result.md5
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment