Skip to content

Instantly share code, notes, and snippets.

@alexclifford
Created January 23, 2014 23:08
Show Gist options
  • Save alexclifford/8588689 to your computer and use it in GitHub Desktop.
Save alexclifford/8588689 to your computer and use it in GitHub Desktop.
Reformat an SD card
#!/bin/sh
TARGETDISK="/dev/mmcblk0"
TARGETPARTITION="p1"
MOUNTPOINT="/media/mmcblk0p1"
# Unmount the current SD card
echo "Unmounting SD card..."
umount $MOUNTPOINT
echo ""
# Delete the current partition on the disk (there is only 1)
echo "Deleting existing partition..."
echo "d
w" | fdisk $TARGETDISK
echo ""
# Re-create FAT32 partition
echo "Re-creating FAT32 partition..."
echo "n
p
1
t
b
w" | fdisk $TARGETDISK
echo ""
echo "Waiting 5 seconds for partition to prepare..."
sleep 5
echo ""
echo "Unmounting new partition and creating the DOS file system..."
umount $MOUNTPOINT
mkdosfs -v $TARGETDISK$TARGETPARTITION
echo ""
echo "Re-mounting the new, fresh file system..."
sleep 2
mount $TARGETDISK$TARGETPARTITION $MOUNTPOINT
echo "Complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment