How to use the command 'diskutil' (with examples)

How to use the command 'diskutil' (with examples)

  • Osx
  • December 25, 2023

Diskutil is a utility command that is used to manage local disks and volumes on macOS systems. It provides various functionalities such as listing available disks and partitions, repairing filesystem data structures, unmounting volumes, and ejecting CD/DVDs. In this article, we will explore each of these use cases of the ‘diskutil’ command.

Use case 1: List all currently available disks, partitions, and mounted volumes

Code:

diskutil list

Motivation: The ‘diskutil list’ command allows users to view the list of all currently available disks, partitions, and mounted volumes on their macOS system. This can be useful for checking the status and configurations of the disks and volumes.

Explanation: The ‘diskutil list’ command lists all disks, partitions, and mounted volumes. It does not require any additional arguments or options.

Example output:

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *256.1 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk1         255.9 GB   disk0s2

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +255.9 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD            11.0 GB    disk1s1
   2:                APFS Volume Preboot                 81.4 MB    disk1s2
   3:                APFS Volume Recovery                528.6 MB   disk1s3
   4:                APFS Volume VM                      4.3 GB     disk1s4

Use case 2: Repair the filesystem data structures of a volume

Code:

diskutil repairVolume /dev/disk_device

Motivation: The ‘diskutil repairVolume’ command is useful when there are issues with the filesystem data structures of a volume. It can help fix filesystem errors and ensure the integrity of the data stored on the volume.

Explanation: The ‘diskutil repairVolume’ command is used to repair the filesystem data structures of a specific volume. The argument ‘/dev/disk_device’ should be replaced with the identifier of the volume to be repaired.

Example output (if repair is needed):

Started file system repair on disk1s2 Preboot
Updating boot support partitions for the volume as required
File system repair complete; boot support partitions now present (Run
diskutil mount for these changes to take effect)
Finished file system repair on disk1s2 Preboot

Example output (if no repair is needed):

Volume Macintosh HD appears to be OK

Use case 3: Unmount a volume

Code:

diskutil unmountDisk /dev/disk_device

Motivation: Unmounting a volume is necessary when you want to disconnect the volume from the file system, allowing it to be safely removed or relocated. The ‘diskutil unmountDisk’ command provides a way to unmount a volume from the macOS system.

Explanation: The ‘diskutil unmountDisk’ command is used to unmount a specific disk. The argument ‘/dev/disk_device’ should be replaced with the identifier of the disk to be unmounted.

Example output:

Unmount of all volumes on disk1 was successful

Use case 4: Eject a CD/DVD (unmount first)

Code:

diskutil eject /dev/disk_device1

Motivation: When using a CD/DVD, it is recommended to unmount it first before ejecting it physically. This ensures that all read/write operations are completed and any caches are flushed. The ‘diskutil eject’ command provides a way to eject a CD/DVD smoothly.

Explanation: The ‘diskutil eject’ command is used to eject a specific disk. The argument ‘/dev/disk_device1’ should be replaced with the identifier of the CD/DVD to be ejected.

Example output:

Disk /dev/disk2 ejected

Conclusion

In this article, we have explored various use cases of the ‘diskutil’ command. It is a powerful utility that allows users to manage disks, partitions, and volumes on macOS systems. You can use it to list available disks and volumes, repair filesystem data structures, unmount volumes, and eject CD/DVDs. Understanding how to utilize these use cases will help you effectively manage your storage devices on macOS.

Related Posts

How to use the command 'tailscale ssh' (with examples)

How to use the command 'tailscale ssh' (with examples)

Tailscale is a tool that provides secure networking across multiple devices and networks.

Read More
Managing VirtualBox Virtual Machines (with examples)

Managing VirtualBox Virtual Machines (with examples)

VirtualBox is a powerful virtualization software that allows users to run multiple operating systems on a single physical machine.

Read More
How to use the command 'Clear-RecycleBin' (with examples)

How to use the command 'Clear-RecycleBin' (with examples)

The ‘Clear-RecycleBin’ command is a PowerShell command that allows users to clear items from the Recycle Bin.

Read More