# fdisk — Partition Disks with fdisk and parted

> Partition disks on Linux: create and edit partition tables with fdisk and parted, build filesystems and set up swap.

Source: https://www.jpkc.com/db/en/cheatsheets/shell-system/fdisk/

<!-- PROSE:intro -->
fdisk is the classic tool for editing partition tables on Linux interactively – modern versions handle both MBR and GPT. parted complements it with native GPT support and the ability to grow or shrink partitions after the fact. Both reach deep into your storage devices: pick the wrong device or write too hastily, and your data becomes unreachable. So **always** confirm which device you are working on with `lsblk` or `fdisk -l`, and back up the partition table beforehand with `sfdisk -d`.
<!-- PROSE:intro:end -->

## fdisk — View Partitions

`fdisk -l` — List all disks and their partition tables.

```bash
sudo fdisk -l
```

`fdisk -l <device>` — List partitions on a specific disk.

```bash
sudo fdisk -l /dev/sda
```

`fdisk -x <device>` — Show extended/expert partition info.

```bash
sudo fdisk -x /dev/sda
```

## fdisk — Partition Management

`fdisk <device>` — Start interactive partitioning on a device.

```bash
sudo fdisk /dev/sdb
```

`n` — Create a new partition (inside fdisk).

```bash
Command: n → primary → partition number → first/last sector
```

`d` — Delete a partition (inside fdisk).

```bash
Command: d → partition number
```

`t` — Change partition type (inside fdisk).

```bash
Command: t → partition number → type code (83=Linux, 82=swap)
```

`p` — Print the partition table (inside fdisk).

```bash
Command: p
```

`w` — Write changes and exit (inside fdisk).

```bash
Command: w
```

`q` — Quit without saving (inside fdisk).

```bash
Command: q
```

## parted — View & Create

`parted -l` — List all disks and partitions.

```bash
sudo parted -l
```

`parted <device> print` — Show partition table of a device.

```bash
sudo parted /dev/sda print
```

`parted <device> mklabel <type>` — Create a new partition table (gpt or msdos).

```bash
sudo parted /dev/sdb mklabel gpt
```

`parted <device> mkpart <name> <fs> <start> <end>` — Create a partition.

```bash
sudo parted /dev/sdb mkpart primary ext4 1MiB 100%
```

`parted <device> rm <number>` — Remove a partition by number.

```bash
sudo parted /dev/sdb rm 2
```

## parted — Resize & Modify

`parted <device> resizepart <number> <end>` — Resize a partition (grow or shrink).

```bash
sudo parted /dev/sda resizepart 2 100%
```

`parted <device> name <number> '<name>'` — Set a partition name (GPT only).

```bash
sudo parted /dev/sda name 1 'boot'
```

`parted <device> set <number> boot on` — Set the boot flag on a partition.

```bash
sudo parted /dev/sda set 1 boot on
```

`parted <device> set <number> lvm on` — Mark a partition for LVM.

```bash
sudo parted /dev/sdb set 1 lvm on
```

## Filesystem Creation

`mkfs.ext4 <partition>` — Create an ext4 filesystem.

```bash
sudo mkfs.ext4 /dev/sdb1
```

`mkfs.xfs <partition>` — Create an XFS filesystem.

```bash
sudo mkfs.xfs /dev/sdb1
```

`mkfs.vfat -F 32 <partition>` — Create a FAT32 filesystem.

```bash
sudo mkfs.vfat -F 32 /dev/sdb1
```

`mkswap <partition>` — Set up a swap partition.

```bash
sudo mkswap /dev/sdb2
```

`swapon <partition>` — Enable a swap partition.

```bash
sudo swapon /dev/sdb2
```

<!-- PROSE:outro -->
## Conclusion

fdisk and parted are powerful but unforgiving of careless mistakes: target the wrong device and you may wipe an entire drive. Internalise a few safety rules. In fdisk, changes are only written to disk with `w` (write) – until then `q` (quit without saving) is your safety net, and after `w` there is **no** undo. Verify the device name with `lsblk` and `fdisk -l` before every action, and back up the existing partition table with `sfdisk -d /dev/sdX > table.sav` before you change anything. For drives larger than 2 TB and modern UEFI systems, use GPT instead of MBR – fdisk can do this nowadays, though many people still reach for `gdisk` or `parted`. And remember: a freshly created partition is still empty – only `mkfs` creates the filesystem, and doing so erases any data already on it.

## Further Reading

- [ArchWiki: Partitioning](https://wiki.archlinux.org/title/Partitioning) – thorough, distribution-agnostic reference covering MBR, GPT, fdisk and parted
- [GNU Parted Manual](https://www.gnu.org/software/parted/manual/parted.html) – official parted manual with all commands and options
<!-- PROSE:outro:end -->

## Related Commands

- [lsblk](https://www.jpkc.com/db/en/cheatsheets/shell-system/lsblk/) – show disks and partitions as a tree, ideal for a device check before any fdisk action
- [mount](https://www.jpkc.com/db/en/cheatsheets/shell-system/mount/) – mount filesystems once you have partitioned and formatted a disk
- [dd](https://www.jpkc.com/db/en/cheatsheets/shell-system/dd/) – back up or clone disks and partition tables block by block

