Disk formatting & partitioning
Linux Command Line Quick Reference
1 min read
Published Aug 18 2026
Guide Sections
Guide Comments
Formatting and partitioning can destroy data. Confirm the device name with lsblk before running any write operation. A device such as /dev/sdb represents a whole disk, while /dev/sdb1 represents a partition on it.
Inspect and partition disks
lsblk -fsudo fdisk -lsudo parted -lsudo fdisk /dev/sdbsudo parted /dev/sdbfdisk -l lists partition tables. Interactive fdisk can create, delete and change partitions, but changes are not written until you explicitly choose to write them. parted supports GPT and can also be used interactively or scripted.
After creating a partition, make a filesystem on the partition, not normally on the whole disk:
Create filesystems and swap
sudo mkfs.ext4 /dev/sdb1sudo mkfs.xfs /dev/sdb1sudo mkfs.vfat -F 32 /dev/sdb1sudo mkswap /dev/sdb1sudo swapon /dev/sdb1sudo swapoff /dev/sdb1mkfs.ext4 creates an ext4 filesystem. mkfs.xfs creates XFS. mkfs.vfat -F 32 creates FAT32. mkswap prepares swap space and swapon activates it.
Never run a mkfs command against a partition containing data you need. Formatting creates new filesystem structures and makes existing contents inaccessible.