fdisk

Intro

fdisk gives you full control when partitioning hard-drives, ssds, flash-drives, and other disks.

List Available Disks and Partitions

$ sudo fdisk -l
[sudo] password for joe: 
Disk /dev/nvme0n1: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: PM961 NVMe SAMSUNG 512GB                
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 1D01CEB6-E01F-4B03-9C66-A4A697097A0E

Device            Start        End   Sectors   Size Type
/dev/nvme0n1p1     2048    1050623   1048576   512M EFI System
/dev/nvme0n1p2  1050624   26216447  25165824    12G Linux swap
/dev/nvme0n1p3 26216448 1000214527 973998080 464.4G Linux root (x86-64)

Format a Disk

Common situations to want to format a disk with fdisk include

  • Performing a fresh Linux installation
  • Re-partitioning a mangled flash drive

NOTE: While fdisk is a powerful tool, keep in mind that it will let you format and wipe your disks more easily than a GUI tool. Use care so you don't accidentally delete your data!

NOTE: Make sure to double-check you are modifying the correct disk before applying changes.

$ sudo fdisk /dev/sda
...
Command (m for help):
  • Type m to get help for all the actions available in the interface

Key Commands

  • m - print the help menu
  • p - print the current partition table
  • d - delete a partition
  • n - add a new partition
  • q - quit without applying changes
  • w - write partition changes to disk (this can destroy the previous partition scheme!)

Once your partitions are set up, you may want to use mkfs to install a filesystem on it

See Also

beefslab