Linux Command Reference ยท Disks and Filesystems
Linux fdisk and parted commands
Reference for fdisk and parted: list and edit MBR and GPT partition tables, read partition entries, convert sectors to sizes, and avoid destroying data.
A partition table is a small structure at the start of a disk that says "this disk is divided into these regions". fdisk and parted are the standard tools to view and change it. fdisk is a simple interactive editor (it now handles both MBR and GPT); parted can also run non-interactively with commands on the command line, and can resize some filesystems' partitions.
Both tools need root and can destroy data in seconds, and neither is installed in our sandbox. The commands below are read-only text. The live examples build a disk image file with a hand-written partition entry and decode it with od, which shows what these tools actually store.
sudo fdisk -l /dev/sdb # list the table, change nothing sudo parted /dev/sdb print # same with parted sudo fdisk /dev/sdb # interactive editing (writes only on 'w')
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
fdisk [-l] [-u] DEVICE parted [-s] DEVICE [COMMAND...]
Common fdisk interactive keys:
| Key | Action |
|---|---|
p |
Print the partition table. |
n |
New partition (asks for number, first and last sector or +5G). |
d |
Delete a partition. |
t |
Change a partition's type. |
g / o |
Create a new empty GPT / MBR (DOS) table. |
a |
Toggle the bootable flag (MBR). |
w |
Write the changes and exit. |
q |
Quit without saving. |
Common parted commands:
| Command | Description |
|---|---|
print, print free |
Show the table, optionally with free space. |
mklabel gpt / mklabel msdos |
Create a new empty table (destroys the old one). |
mkpart NAME FSTYPE START END |
Create a partition, for example mkpart data ext4 1MiB 100%. |
rm N |
Remove partition N. |
resizepart N END |
Change the end of partition N. |
set N FLAG on |
Set a flag such as boot, esp or lvm. |
-s, --script |
Never ask questions. |
Returns: 0 on success, non-zero on errors. fdisk changes nothing on disk until you press w; parted applies each command immediately.
Usage notes
- MBR versus GPT. MBR (also called DOS) supports 4 primary partitions and disks up to 2 TiB with 512-byte sectors; GPT supports 128 partitions by default, disks beyond 2 TiB and keeps a backup table at the end of the disk. Use GPT for new disks; the first sector of a GPT disk still contains a "protective MBR" so old tools do not think it is empty.
- After changing the table the kernel must re-read it:
partprobe /dev/sdborblockdev --rereadpt. A busy disk (mounted partition) may need a reboot. - The table is not the filesystem. Creating a partition does not format it; the next step is
mkfs. Deleting a partition does not erase the data, so recovery is sometimes possible by recreating the exact same entry. - Wrong device = lost data. Confirm the name with
lsblkbefore every write, and check sizes and models. - Alignment. Start partitions at sector 2048 (1 MiB). Modern tools default to this; misaligned partitions on SSDs and 4K disks are slower.
- Types are labels, not behavior:
0x83is "Linux",0x82swap,0xEFEFI system in MBR; GPT uses GUIDs. Setting a wrong type does not break the data but confuses tools. - Non-interactive use.
fdiskreads commands from stdin (printf 'g\nn\n\n\n\nw\n' | fdisk /dev/sdb), butparted -sis easier to script. Try it on an image file (truncate -s 1G disk.img; parted -s disk.img mklabel gpt) before a real disk. - Alternatives.
sfdiskdumps and restores a table as text (sfdisk -d /dev/sda > table.txt),gdiskis a GPT-only editor, andcfdiskhas a menu interface. - LVM and RAID usually sit on top of partitions or whole disks; see
pvcreate, vgcreate and lvcreate.