7

I'll admit that I am kind of rolling multiple questions in to one here so please forgive me and try not to knock me for it.

I am new to Mac but I'm trying to port one of my Java applications to OS X. On Windows, it uses the hard drive serial number for something and I'm looking to do equivalent in Mac using the Terminal!

The close thing I can find is the volume UUID using diskutil info ..., which is fine (as long as this will always stay the same?): so my main question is which volume I should use, seeing as though three appear when I type diskutil list? Obviously it needs to have a UUID, but I would also like to use the volume that is least likely to change, which would seem to be the one with the operating system on to me!

So I would like to know which volume is the safest for me to use for this purpose, and if it varies on different computers - how to find it!

Thanks in advance

2 Answers 2

11

Open Terminal.app and run df -h /:

% df -h /
Filesystem     Size   Used  Avail Capacity  Mounted on
/dev/disk1s2  111Gi   75Gi   36Gi    68%    /

On my machine my OS drive is on /dev/disk1s2. With this information you can use the Disk Utility app and find out what physical drive your OS is on:

Disk Utility

Using diskutil from command line you're OS drive will be whatever correlates to what the df command output gave you:

% diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Nymeria                 499.2 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *120.0 GB   disk1
   1:                        EFI                         209.7 MB   disk1s1
-> 2:                  Apple_HFS Untitled 1              119.2 GB   disk1s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk1s3

If you're unfamiliar with how UNIX-like operating systems (OS X) partition a drive check out this Wikipedia article.

And if you want to get all the details for the root partition directly in terminal, run

diskutil info $(df / | tail -1 | cut -d' ' -f 1)
6
  • Thank you very much for that information - it is just what I needed. Do you know if there is a way to combine that line with diskutil info ... or do you think I will have to run the command in Java and use what it returns in another command for the diskutil info part?
    – Andy
    Commented May 2, 2012 at 19:38
  • That's outside my field of knowledge, but I'll update the answer if someone leaves a comment.
    – Aaron Lake
    Commented May 2, 2012 at 19:40
  • That's fair enough. I am pretty certain I know what to now any way. Thanks again for your help!
    – Andy
    Commented May 2, 2012 at 19:49
  • 2
    @Andy, are you looking for something like diskutil info $(df -h / | tail -1 | cut -d' ' -f 1)?
    – nohillside
    Commented May 2, 2012 at 21:30
  • @patrix Sorry for the slow reply. Yes, that was what I looking for. Thank you
    – Andy
    Commented May 4, 2012 at 16:18
0

This shell function will tell you the name of the volume that holds any given file (or folder, etc.).

function volumeOfFile {
  local result=$(
      /bin/df "$1" \
      | /usr/bin/sed -e 1d -e 's,  *, ,g' -e s,/Volumes/,, \
      | cut -d' ' -f 9-
  )
  if [[ $result == / ]] ; then
    /bin/ls -l /Volumes \
    | /usr/bin/sed -n -e 's,  *, ,g' -e 's, -> /$,,p' \
    | cut -d' ' -f 9-
  else
    echo "$result"
  fi
}

Examples:

$ volumeOfFile /System
Mac HD
$ volumeOfFile /Volumes/Other/Users
Other
$

I'd like to know of an easier way to do this or a way to do it in Swift.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .