title: Command line to unmount all external drive on OS X
body: <p>I am trying to write a shell script to unmount/mount all external drive on OS X.</p>
  
  <p>My disk list is as follows:</p>
  
  <pre><code>$ diskutil list
  /dev/disk0
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:      GUID_partition_scheme                        *500.3 GB   disk0
     1:                        EFI EFI                     209.7 MB   disk0s1
     2:          Apple_CoreStorage                         499.4 GB   disk0s2
     3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
  /dev/disk1
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:                  Apple_HFS Macintosh HD           *496.3 GB   disk1
                                   Logical Volume on disk0s2
                                   AE949253-803C-49DC-93EA-7DFC74C2EAA2
                                   Unencrypted
  /dev/disk2
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:      GUID_partition_scheme                        *1.0 TB     disk2
     1:                        EFI EFI                     209.7 MB   disk2s1
     2:                  Apple_HFS Time Machine            999.9 GB   disk2s2
  /dev/disk3
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:      GUID_partition_scheme                        *750.2 GB   disk3
     1:                        EFI EFI                     209.7 MB   disk3s1
     2:       Microsoft Basic Data USB HD 4                749.9 GB   disk3s2
  /dev/disk4
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:      GUID_partition_scheme                        *500.1 GB   disk4
     1:                        EFI EFI                     209.7 MB   disk4s1
     2:       Microsoft Basic Data Backup                  499.9 GB   disk4s2
  /dev/disk5
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:      GUID_partition_scheme                        *1.0 TB     disk5
     1:                        EFI EFI                     209.7 MB   disk5s1
     2:                  Apple_HFS USB HD 1                999.9 GB   disk5s2
  /dev/disk6
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:      GUID_partition_scheme                        *4.0 TB     disk6
     1:                        EFI EFI                     209.7 MB   disk6s1
     2:                  Apple_HFS USB HD 2                4.0 TB     disk6s2
  /dev/disk8
     #:                       TYPE NAME                    SIZE       IDENTIFIER
     0:     Apple_partition_scheme                        *122.9 GB   disk8
     1:        Apple_partition_map                         32.3 KB    disk8s1
     2:             Apple_Driver43                         28.7 KB    disk8s2
     3:             Apple_Driver43                         28.7 KB    disk8s3
     4:           Apple_Driver_ATA                         28.7 KB    disk8s4
     5:           Apple_Driver_ATA                         28.7 KB    disk8s5
     6:             Apple_FWDriver                         262.1 KB   disk8s6
     7:         Apple_Driver_IOKit                         262.1 KB   disk8s7
     8:              Apple_Patches                         262.1 KB   disk8s8
     9:                  Apple_HFS Windows                 122.9 GB   disk8s9
  </code></pre>
  
  <p>So, I write a script like:</p>
  
  <pre><code>#!/bin/sh
  
  diskutil list | grep -oE 'disk[0-9]s2' | while read i
  do
    Prtl=`diskutil info ${i} | grep Protocol | cut -d ' ' -f 21`
    if [ ${Prtl} = USB ]; then
      diskutil unmount ${i}
    fi
  done
  </code></pre>
  
  <p>I'd like to know the followings:</p>
  
  <p>1.Is there a way to identify external drive without using diskutil info?  The reason is diskutil info starts up sleeping drive, my script is very slow.  </p>
  
  <p>2.Are there any much more smart way?  I have Googled so much, but I can't find a way which I want.  Hope to help me...</p>