fdisk - macOS


Overview

fdisk is a command-line utility used on macOS for disk partitioning. It allows users to create, modify, and delete disk partitions on a hard drive. This tool is primarily used by system administrators and power users to manage disk space effectively or prepare a disk for a specific type of installation or setup.

Syntax

The basic syntax of the fdisk command is as follows:

fdisk [options] device

Where device is the path to the disk device (e.g., /dev/disk2).

Options/Flags

Here are some of the most commonly used options in fdisk:

  • -i: Initializes the disk with a new MBR (Master Boot Record).
  • -u: Updates the MBR code on the specified disk without altering the partition table.
  • -e: Enters an interactive mode where detailed disk editing can be performed.
  • -d: Dumps the current partition table to stdout.
  • -y: Confirms all prompts automatically (useful for scripting).
  • -r: Reads the partition table and verifies its integrity.

Each option has a specific use case, such as -i for initializing new disks or -e for meticulous partition management.

Examples

  1. Viewing the Partition Table:

    fdisk /dev/disk2 -d
    

    This command displays the partition table of the disk at /dev/disk2.

  2. Initializing a Disk:

    fdisk -i /dev/disk2
    

    Initializes /dev/disk2 with a new master boot record.

  3. Interactive Partition Editing:

    fdisk -e /dev/disk2
    

    Launches an interactive session to edit the partition table of /dev/disk2.

Common Issues

  • Permission Denied: Ensure you’re running fdisk with sufficient privileges, using sudo if necessary.
  • Incorrect Device Path: Double-check the device path. Mistyping this can lead to operating on the wrong disk.
  • Data Loss: Improper usage of fdisk can lead to data loss. Always back up data before modifying disk partitions.

Integration

fdisk can be integrated with other commands to automate disk management tasks. For example, to format a newly created partition:

fdisk -i /dev/disk3
newfs_hfs /dev/disk3s1

This script initializes /dev/disk3 and formats the first partition with the HFS filesystem.

  • diskutil: A more user-friendly command for managing disks in macOS. Often preferred for its simpler syntax and powerful features.
  • gpt: Low-level utility for GUID Partition Table manipulation.

Additional resources and detailed documentation can be found in the man pages (man fdisk) or on the Apple Developer Documentation website.