How to mount and unmount drives in macOS and OS X from the command line

- by

Unmounting external drives on a Mac is usually done quick and simple by either dragging drive icon to the trash, or by using the eject symbol in a Finder window. Mounting usually happens automatically when a new drive is inserted into a USB port or SD card slot.

However, there is a way to do this via the command line, of which I am a big fan. Fire up a Terminal session and see how to do it.

Listing available drives

To see what’s currently attached to your Mac, let’s use the diskutil command, followed by the word list. You’ll see output like this:

diskutil list

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *512.1 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh SSD           511.3 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

/dev/disk1 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk1
   1:                        EFI EFI                     209.7 MB   disk1s1
   2:                  Apple_HFS Mac HDD 1TB             999.9 GB   disk1s2

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *1.0 TB     disk2
   1:                  Apple_HFS VM Drive 

Attached drives are listed with their physical locations on the left (i.e. /dev/disk0, /dev/disk1, etc), as well as with their respective partitions if available on the right (like disk0s1, disk1s2, etc). Make a mental note of the latter: you’ll see that we have a physical disk (like disk0), on which several partitions may have been created. It is those partitions we’ll mount and unmount, NOT the physical drive.

Unmounting an attached hard drive

On my system I have two internal hard disks (disk0 and disk1), and one external USB drive (disk2). Let’s unmount that USB drive now:

diskutil unmount /dev/disk2s1

Volume VM Drive on disk2s1 unmounted

Note how we use the unmount command. We need to specify the location of the partition with its full path (i.e. /dev/disk2s1).

Mounting an attached hard drive

To mount the drive again, without having to take it out and plugging it in again, I can issue this command:

diskutil mount /dev/disk2s1

Volume VM Drive on /dev/disk2s1 mounted


If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

2 thoughts on “How to mount and unmount drives in macOS and OS X from the command line”

  1. Hi, Jay! How can I execute unmount script when I close the lid (sleep) of my mac and execute mount script when I open the lid (wakeup)?

  2. Hi Barry – oh, that’s a good question. I wish I knew, it would be a very useful option indeed. Sadly I don’t know the answer, but let’s keep our eyes and ears open. If I find a way to do it, I’ll let you know. If you find a solution, please share it here.

    All the best,

    JAY

Leave a Reply to Jay VersluisCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.