fdisk

system managementLinux/Unix
The fdisk command is one of the most frequently used commands in Linux/Unix-like operating systems. fdisk Manipulate disk partition table

Quick Reference

Command Name:

fdisk

Category:

system management

Platform:

Linux/Unix

Basic Usage:

fdisk [options] [arguments]

Common Use Cases

    Syntax

    fdisk [options] [device]

    Options

    Option Description
    -b, --sector-size Specify the sector size of the disk
    -B, --protect-boot Don't erase the bootbits when creating a new label
    -c, --compatibility[=] Set compatibility mode (dos or nondos)
    -L, --color[=] Control color output (auto, always, or never)
    -l, --list List partition table for specified devices
    -x, --list-details Like --list but with more details
    -n, --noauto-pt Don't automatically create a default partition table on empty devices
    -o, --output Specify output columns for the table view
    -t, --type Specify the partition table type (dos, gpt, etc.)
    -u, --units[=] Display units (cylinders, sectors, or specify custom)
    -s, --getsz Show the size of the device in sectors
    -w, --wipe Wipe signatures when writing a new table (auto, always, or never)
    -W, --wipe-partitions Wipe signatures from new partitions (auto, always, or never)
    -C, --cylinders Specify the number of cylinders
    -H, --heads Specify the number of heads
    -S, --sectors Specify the number of sectors per track

    Examples

    How to Use These Examples

    The examples below show common ways to use the fdisk command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    sudo fdisk -l
    List all partitions on all disks.
    sudo fdisk -l /dev/sda
    List all partitions on the specified disk (/dev/sda).
    sudo fdisk /dev/sda
    Enter interactive fdisk mode for the specified disk. # Advanced Examples Advanced sudo fdisk -l -u /dev/sda List all partitions with sector size units. sudo fdisk -o device,start,end,size,type -l List partitions with specific columns. sudo fdisk -c=dos /dev/sda Specify DOS compatibility mode. sudo fdisk -w=always /dev/sda Always write the partition table when exiting. echo -e "p\nq" | sudo fdisk /dev/sda Print the partition table and quit (non-interactive).

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The fdisk command is a text-based utility for viewing and manipulating disk partition tables. It's one of the most powerful and commonly used tools for disk partitioning in Linux. Key points about fdisk: 1. Root Privileges: fdisk requires root privileges (sudo) to make changes to disk partitions. 2. Interactive Mode: When run without the -l option, fdisk enters an interactive mode where you can use single-letter commands to perform various operations. 3. Common interactive commands include: - p: print the partition table - n: create a new partition - d: delete a partition - t: change a partition's type - w: write changes to disk and exit - q: quit without saving changes 4. Limitations: Traditional fdisk has some limitations: - It doesn't work with GPT partition tables (use gdisk or parted instead) - It doesn't modify the filesystem, only the partition table - It doesn't resize existing partitions (use resize2fs, lvresize, etc.) 5. Safety: fdisk modifies the partition table directly, which can result in data loss if used incorrectly. Always back up important data before using fdisk. 6. Post-fdisk steps: After creating or modifying partitions with fdisk, you typically need to: - Create filesystems on new partitions (mkfs) - Update the kernel's partition table (partprobe) - Mount the filesystems to make them accessible (mount) 7. Modern alternatives: While fdisk is still widely used, modern systems often use tools like parted, gdisk, or GUI tools that support advanced features like GPT partitioning and resizing.

    Related Commands

    These commands are frequently used alongside fdisk or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the fdisk command works in different scenarios.

    $ fdisk
    View All Commands