How can I get the mount path of a USB device on OSX?

61

15

I have a backup script that backs up some data to a USB device. The problem I have is that OSX sometimes changes the expected mount path. For example if some file is locked under the expected mount path, OSX mounts it on another path. A USB device named 'BACKUP' can be mounted at /Volumes/BACKUP-1 instead of /Volumes/BACKUP.

Is there a way to finding out the current mount path of a USB device in the OSX Terminal? Something like 'mount_path BACKUP' (command is fake) which would then return '/Volumes/BACKUP-1' or nothing if the device was not mounted?

xastor

Posted 2012-05-26T09:09:09.103

Reputation: 739

Answers

72

The following commands show you information about mounted volumes:

  • The well-known Unix mount, showing e.g. /dev/disk5s3 mounted at /Volumes/Foo
  • diskutil list shows an overview of all disks and volumes
  • diskutil info /dev/disk5s3 shows information about that volume, including a Volume UUID that can be used to uniquely identify that volume.

You can query diskutil info by using the volume's UUID:

$ diskutil info DEC8759E-F77D-3EAE-B3EB-B6438F1AA428 | grep 'Mount Point'
   Mount Point:              /Volumes/DroboOne

Sample command output on my system:

$ mount
/dev/disk1 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
localhost:/bNqIvVr1ZdFBdf19Io81Q4 on /Volumes/MobileBackups (mtmfs, nosuid, read-only, nobrowse)
/dev/disk4 on /Volumes/MyBook (hfs, local, nodev, nosuid, journaled)
/dev/disk5s3 on /Volumes/DroboOne (hfs, local, nodev, nosuid, journaled, noowners)
/dev/disk7s3 on /Volumes/DroboTwo (hfs, local, nodev, nosuid, journaled, noowners)
/dev/disk6s3 on /Volumes/DroboThree (hfs, local, nodev, nosuid, journaled, noowners)

$ diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *256.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:          Apple_CoreStorage                         240.0 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Servus10 HD            *239.7 GB   disk1
/dev/disk2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *3.0 TB     disk2
   1:                        EFI                         209.7 MB   disk2s1
   2:          Apple_CoreStorage                         3.0 TB     disk2s2
   3:                 Apple_Boot Boot OS X               134.2 MB   disk2s3
/dev/disk4
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS MyBook                 *3.0 TB     disk4
/dev/disk5
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     Apple_partition_scheme                        *2.2 TB     disk5
   1:        Apple_partition_map                         32.3 KB    disk5s1
   2:                  Apple_HFS DroboOne                2.2 TB     disk5s3
/dev/disk6
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     Apple_partition_scheme                        *2.2 TB     disk6
   1:        Apple_partition_map                         32.3 KB    disk6s1
   2:                  Apple_HFS DroboThree              2.2 TB     disk6s3
/dev/disk7
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     Apple_partition_scheme                        *2.2 TB     disk7
   1:        Apple_partition_map                         32.3 KB    disk7s1
   2:                  Apple_HFS DroboTwo                2.2 TB     disk7s3

$ diskutil info /dev/disk5s3
   Device Identifier:        disk5s3
   Device Node:              /dev/disk5s3
   Part of Whole:            disk5
   Device / Media Name:      Untitled

   Volume Name:              DroboOne
   Escaped with Unicode:     DroboOne

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

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

   Partition Type:           Apple_HFS
   OS Can Be Installed:      No
   Media Type:               Generic
   Protocol:                 FireWire
   SMART Status:             Not Supported
   Volume UUID:              DEC8759E-F77D-3EAE-B3EB-B6438F1AA428

   Total Size:               2.2 TB (2198888927232 Bytes) (exactly 4294704936 512-Byte-Blocks)
   Volume Free Space:        169.4 GB (169412173824 Bytes) (exactly 330883152 512-Byte-Blocks)
   Device Block Size:        512 Bytes

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

   Whole:                    No
   Internal:                 No

Daniel Beck

Posted 2012-05-26T09:09:09.103

Reputation: 98 421

This old reply just helped me! "diskutil info /dev/disk5s3" to convert the /dev/(foo) to a volume. Many thanks! – Dragick – 2016-01-25T01:01:18.183

I was hoping to avoid having to use those commands and parsing my way through this :) But if there is no other way, do you know of any bash functions that do this for you? – xastor – 2012-05-26T11:31:48.503

@xastor Added relevant info. man diskutil would have helped. – Daniel Beck – 2012-05-26T11:38:53.567

Thanks, I wrote a script now and it works. I was hoping there was another way though, I should have mentioned that in the question. For example, what happens on an OSX system with another language? I cannot grep 'Mount Volume' on a french system I suppose.. – xastor – 2012-05-26T12:01:14.940

@xastor diskutil is not localized. – Daniel Beck – 2012-05-26T12:03:44.330

1This is a dependable solution then. Thank you! – xastor – 2012-05-26T12:05:35.980

@xastor Alternatively, just grep for /Volumes/ and ignore the label. It's always there unless you roll your own solution. – Daniel Beck – 2012-05-26T12:06:03.530

10

What about this command:

df -lH | grep "Filesystem"; df -lH | grep "/Volumes/*"

In the column "Mounted on" you get all the Mount Points of all devices mounted on "/Volumes", which in my case are almost always USB devices ;-)

The grep commands basically skip the hard drive which is mounted on "/".

In the terminal of my OSX Snow Leopard, I use it for quick overview of the Mount Points of my currently connected USB Devices. If you are only interested in Mount Points and not all the other parameters as UUID etc., this would be in my opinion the more straight forward way rather than "diskutil" with all its information.

mallin

Posted 2012-05-26T09:09:09.103

Reputation: 103

2

I retrieve it in a variable:

media=\`df | grep "media" | awk '{print $6}'\`

or

media=$(df | awk '/media/ {print $6}')

The df command lists the partitions, the resulting output is piped as input to the grep command which filters and keeps only the lines containing the word media, which is then piped to the awk command which only keeps the 6th column of its one line input.

Stephane

Posted 2012-05-26T09:09:09.103

Reputation: 161

The second form should be much preferred, but it's not obvious how to pass in the /media/ regex parametrized. Try media=$(df | awk -v regex="$regex" '$1 ~ regex { print $6 }') to pass in the shell variable $regex as the thing to search for. – tripleee – 2017-05-03T04:15:11.823

1

Just the good old diskutil. This isn't Linux, where you can probably peek into /sys or something.

diskutil info "$VolumeName" | grep "Mount Point" | tr " " "\n" | tail -1

Haotian Yang

Posted 2012-05-26T09:09:09.103

Reputation: 31

0

I would just use fstab for this. There's a thread regarding this topic here on Super User: Mac Lion: fstab is deprecated. so what replaces it to prevent a partition from mounting?

dag729

Posted 2012-05-26T09:09:09.103

Reputation: 1 894

2And how would you use fstab for this, exactly? – Daniel Beck – 2012-05-26T13:01:07.613

Taking the UUId of the USB drive, creating a mount point and giving all of it to /etc/fstab. Whenever the aforementioned USB drive is attached, will be mounted on that mount point, recognized by its UUID. – dag729 – 2012-05-26T20:30:09.573

Note that the user's problem comes from the default mount point already being in use due. – Daniel Beck – 2012-05-26T21:57:13.177

That's what I meant: giving the OS a mount point by using /etc/fstab. From that time on, the system will read /etc/fstab, find that there's a UUID with an associated mount point, and it will use that instead of the default one. – dag729 – 2012-05-27T19:46:30.007

0

I ended up using this bash script :

#!/bin/sh
#
# Retrieves the mount point of an OSX volume name or UUID.
# @param $1 Name of the volume or UUID of the volume.
# @return returns the mount path or an empty string if the volume is not mounted. 
#
diskutil info $1 | grep 'Mount Point' | cut -d : -f 2 | sed 's/^ *//g' | sed 's/ *$//g';

xastor

Posted 2012-05-26T09:09:09.103

Reputation: 739

1

If you are using sed anyway, the grep and the cut are useless; diskutil info "$1" | sed -n '/^ *Mount Point: */!d;s///;s/ *$//p'

– tripleee – 2017-05-03T04:11:25.223

This was last September, and nobody's commented on it yet? your 'Mount Point' SHOULD instead be 'Device Node' if you expect this script to do what the comments say it does. – None – 2013-01-22T09:45:49.503

1@Blakat The device node is not what the OP was asking for. The mount point is /Volumes/…, which is what everyone was talking about if I read this question correctly. – slhck – 2013-01-22T09:54:14.413

0

This is what I use in my shell scripts on OS X

df | awk '{print $6}' | grep -Ex "/Volumes/myvolume"

DannyRe

Posted 2012-05-26T09:09:09.103

Reputation: 1 373

Can you add a few sentences to explain how this works? Thanks. – fixer1234 – 2017-04-27T19:10:10.077

This is a roundabout way of saying df | awk '$6 == "/Volumes/myvolume" { print $6 }' i.e. print the volume path if it is exactly the expected one. The grep -E flag is superfluous here and the grep is useless because Awk already knows quite well how to match a regular expression.

– tripleee – 2017-05-02T13:11:45.440

A more useful command would probably grep for the volume before printing only the sixth field, or even better change the first $6 to $1 in my previous comment. – tripleee – 2017-05-03T04:16:35.800

-1

This might work better:

df -lH | grep -E "*putinyourvolumelabel*$" | awk '{print $1}''

Dennis Eisen

Posted 2012-05-26T09:09:09.103

Reputation: 1

6Why might that work better? – bertieb – 2017-04-27T14:06:28.707

Dennis, welcome to Super User. A couple of the other answers aren't good examples, but the goal of answers is to educate rather than just provide cut and paste code. I'm not a coder, so I can't address how good your solution might be, but I suspect bertieb's comment, and perhaps the downvote, go to the fact that this is just unexplained code, rather than to the quality of solution. Can you add a few sentences to explain what this does, and what makes it a better solution than whatever you were comparing it to? Thanks. – fixer1234 – 2017-04-27T19:08:57.103

This code only reads me a strange poem – Samy Bencherif – 2020-02-21T21:31:24.427