How to check installed OS X version from the command-line on each partition?

The following command will give you the OS X version installed on volumeName:

defaults read /Volumes/volumeName/System/Library/CoreServices/SystemVersion ProductVersion

To get a list of each volume's OS version, you need a simple script that will get a list of volumes, search each one for the SystemVersion.plist, and report any that are found.

I found a script on on Mac OS X Hints, by Hal Itosis, that does just that (reposted below for convenience).

#!/bin/bash -
PATH=/bin:/usr/bin
export PATH
IFS=$'\n'

vList=$(df -l | sed '1d;s:^.*%[[:space:]]*::')
sPath=$(find -x /Volumes -maxdepth 1 -type l -not -inum 2)
pFile=/System/Library/CoreServices/SystemVersion

SysVers ()
{
   if [ -e "${1}${pFile}.plist" ]; then
      n="$1"; if [ "$1" = '/' ]; then n="$sPath"; fi
      x=`defaults read ${1}${pFile} ProductVersion`
      b=`defaults read ${1}${pFile} ProductBuildVersion`
      printf '%-16s %-8s %-8s\n' "${n##/*/}" "$x" "($b)"
   fi
}

if [ $# -eq 0 ]; then for v in $vList; do SysVers "$v"; done
else while [ $# -gt 0 ]; do SysVers "$1"; shift; done; fi
exit

You can use the diskutil command in terminal, which roughly speaking provides the ability to script up and perform the same actions as you can in the GUI version.

Using this it is possible to create a script that trawls for the info you need, but you have to be careful in specifying which disks you look at - if you do a blanket check on all disks, you will get disk images and all sorts included if you are not careful, plus you will end up with the recovery partitions and so on etc.

Also, the disk utility only provides the partition format, although that gives you a hint on what you can subsequently search on, i.e. look for a file called "mach_kernel" and chances are it's got OS X on it, it's it's got a "/windows" directory...etc etc, even then you will find partitions that are formatted on Mac or Win formats with no OS installed, so all in all not a trivial task. If you know where the OSX/Windows installer logs are you may be able to insert such logic as to pull out a service pack level, or a patch level etc, but I can't find out how to do that just yet.

Dans-iMac:~ stuffe$ diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *640.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            639.3 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     Apple_partition_scheme                        *18.7 MB    disk1
   1:        Apple_partition_map                         32.3 KB    disk1s1
   2:                  Apple_HFS SMARTReporter           18.7 MB    disk1s2
Dans-iMac:~ stuffe$ diskutil info disk0s2
   Device Identifier:        disk0s2
   Device Node:              /dev/disk0s2
   Part of Whole:            disk0
   Device / Media Name:      Customer

   Volume Name:              Macintosh HD
   Escaped with Unicode:     Macintosh%FF%FE%20%00HD

   Mounted:                  Yes
   Mount Point:              /
   Escaped with Unicode:     /

   File System Personality:  Journaled HFS+
   Type (Bundle):            hfs
   Name (User Visible):      Mac OS Extended (Journaled)
   Journal:                  Journal size 49152 KB at offset 0x129d000
   Owners:                   Enabled

   Partition Type:           Apple_HFS
   OS Can Be Installed:      Yes
   Media Type:               Generic
   Protocol:                 SATA
   SMART Status:             Verified
   Volume UUID:              7B3DEC51-1C86-37B5-95CE-888AE0EBD610

   Total Size:               639.3 GB (639275270144 Bytes) (exactly 1248584512 512-Byte-Blocks)
   Volume Free Space:        343.5 GB (343520002048 Bytes) (exactly 670937504 512-Byte-Blocks)
   Device Block Size:        512 Bytes

   Read-Only Media:          No
   Read-Only Volume:         No
   Ejectable:                No

   Whole:                    No
   Internal:                 Yes
   Solid State:              No

Dans-iMac:~ stuffe$ mountpoint=`diskutil info disk0s2 | grep "Mount Point" | awk '{print $3}'`
Dans-iMac:~ stuffe$ ls -l $mountpoint | grep -E "mach_kernel|windows"
-rw-r--r--@  1 root  wheel  15565404 10 Aug  2011 mach_kernel
Dans-iMac:~ stuffe$ 

Putting all this together in a suitable loop that identifies all the disks you are interested in, removing disk images USB sticks etc, and setting to either run remotely via SSH or whatever....is left as an exercise for the interested...


Here is sample one-liner which gets the version info from SystemVersion.plist:

ex -s +'%s/<[^>].\{-}>//ge' +'%s/\s\+//e' +'%norm J' +'g/^$/d' +%p +q! /System/Library/CoreServices/SystemVersion.plist

You may use head, tail or awk for further parsing, for example to get ProductVersion, try adding:

| grep ^ProductVersion | cut -d' ' -f2

Here is the alternative approach:

egrep "<string|key>" /System/Library/CoreServices/SystemVersion.plist | paste - -