Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Mac OSX Terminal – Format Disk

Posted on June 11, 2019June 11, 2019 By David Kittell

This process is very helpful to format a disk that has multiple partitions on it like a Raspberry Pi SD Card.

This script can be updated to allow a different type of format (JHFS+ instead of FAT32) but for now it serves my purpose.

#!/bin/sh

#  FormatSDCard.sh
#
#
#  Created by David Kittell on 6/11/19.
#

diskutil list | grep '/dev/disk' -A2

# Ask User what disk should be formatted
# say "What disk would you like to format?. Copy/Paste or type in the disk path followed by [ENTER]"
printf "What disk would you like to format?\nCopy/Paste or type in the disk path (ex. /dev/disk2) followed by [ENTER]:\n"
read sDisk
#echo "$sDisk provided"

if [ ! -z "$sDisk" ]
then
  sDiskSpace="$(diskutil list | grep $sDisk -A2 | grep -v $sDisk | grep -v 'SIZE' | tr -s " " | cut -d ' ' -f4 | sed 's|[+*]||g') $(diskutil list | grep $sDisk -A2 | grep -v $sDisk | grep -v 'SIZE' | tr -s " " | cut -d ' ' -f5)"
  echo $sDiskSpace

  clear

  # Ask User if they are sure they want to format the disk
  # say "Are you sure you want to format $sDisk ($sDiskSpace), if yes type y or if no type n followed by [ENTER]"
  printf "Are you sure you want to format $sDisk ($sDiskSpace), if yes type y or if no type n followed by [ENTER]:\n"
  read sDiskConfirm
  #echo "$sDiskConfirm"

  case $sDiskConfirm in
    [yY])

      # Ask User if they are sure they want to format the disk
      # say "Do you want to do a secure format $sDisk ($sDiskSpace), if yes type y or if no (regular format) type n followed by [ENTER]"
      printf "Do you want to do a secure format $sDisk ($sDiskSpace), if yes type y or if no (regular format) type n followed by [ENTER]:\n"
      read sDiskFormatType
      #echo "$sDiskFormatType"

      case $sDiskFormatType in
        [yY])
          sudo diskutil unmountDisk $sDisk && say "Unmount of disk completed"
          # sudo dd if=/dev/urandom of=$sDisk bs=1000000 && say "Disk Ready"
          sudo diskutil zeroDisk $sDisk && say "Disk Ready"
          sudo diskutil secureErase "ms-dos fat32" EMPTY MBRFormat $sDisk && say "Disk Is Formatted" # If you want to securely format the disk
          ;;
        [nN])
          sudo diskutil unmountDisk $sDisk && say "Unmount of disk completed"
          # sudo dd if=/dev/urandom of=$sDisk bs=1000000 && say "Disk Ready"
          sudo diskutil zeroDisk $sDisk && say "Disk Ready"
          sudo diskutil eraseDisk "ms-dos fat32" EMPTY MBRFormat $sDisk && say "Disk Is Formatted"
          ;;
        *)
          clear
          # say "You did not provide a choice. Format of $sDisk ($sDiskSpace) aborted."
          echo "You did not provide a choice\nFormat of $sDisk ($sDiskSpace) aborted."
          ;;
      esac
      ;;
    [nN])
      clear
      # say "Format of $sDisk ($sDiskSpace) aborted."
      echo "Format of $sDisk ($sDiskSpace) aborted."
      ;;
    *)
      clear
      # say "You did not confirm. Format of $sDisk ($sDiskSpace) aborted."
      echo "You did not confirm\nFormat of $sDisk ($sDiskSpace) aborted."
      ;;
  esac
else
  clear
  # say "You did not provide a disk, aborting script"
  echo "You did not provide a disk, aborting script"
fi
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code Mac OS X Shell Mac OSX UNIX UNIX Shell Scripts

Post navigation

Previous post
Next post

Related Posts

Azure – MSSQL Server Information

Posted on May 13, 2016

Same as MSSQL Server Information but is specific to what will work in Azure — Basic Information SELECT @@Servername AS [Server Name] –,@@ServiceName AS [Service Instance] ,Name AS [Database Name] ,create_date AS [Service Started] ,DATEDIFF(s, create_date, GETDATE()) / 86400.0 AS [Days Running] ,DATEDIFF(s, create_date, GETDATE()) AS [Seconds Runnig] ,@@VERSION AS…

Read More

PowerShell – Get/Set Windows Date/Time from NTP

Posted on December 3, 2015February 12, 2016

Sometimes w32Time is not available or practical so we get this PowerShell script to pull NTP date/time and set the Windows Date/Time <# Majority of code is from: Chris Warwick, @cjwarwickps, August 2012 chrisjwarwick.wordpress.com #> $sNTPServer = ‘pool.ntp.org’ function Get-NTPDateTime ([string] $sNTPServer) { $StartOfEpoch=New-Object DateTime(1900,1,1,0,0,0,[DateTimeKind]::Utc) [Byte[]]$NtpData = ,0 * 48…

Read More

Unix – Add User Group To Sudousers

Posted on August 5, 2016

usergroup="developers" su # type in sudo password chmod -v +w /etc/sudoers && echo -e "%$usergroup ALL=(ALL) ALL" >> /etc/sudoers && chmod -v -w /etc/sudoers All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes