Formatting disks and partitions in Linux prepares storage for reliable data use, removes obsolete filesystem structures, and aligns space usage with new workloads. Correct formatting is especially important when reusing drives from other systems or converting removable media for use with Linux tools. Choosing an appropriate filesystem also affects performance, maximum file sizes, and compatibility with other operating systems.
Under Linux, the family of mkfs utilities creates filesystems on block devices such as /dev/sda2 or /dev/sdb1. Each filesystem type has a dedicated helper, for example mkfs.ext4 for ext4, mkfs.btrfs for Btrfs, or mkfs.vfat for FAT32 and exFAT. The selected helper writes superblocks, allocation tables, journals, and other on-disk structures that define how files and directories are stored.
Formatting always overwrites an existing filesystem and destroys the data stored on it. Confirming the correct device name before running any mkfs command avoids damaging the system disk or other important volumes. Unmounting the target partition and ensuring that no services depend on it reduces the risk of corruption and guarantees a clean filesystem layout.
Steps to format disk and partition in Linux:
- Open a terminal with sudo privileges.
- List available disks and partitions and note device names and sizes.
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS loop0 7:0 0 512M 0 loop /mnt/bench loop1 7:1 0 300M 0 loop /mnt/format-prep nbd0 43:0 0 0B 0 disk nbd1 43:32 0 0B 0 disk nbd2 43:64 0 0B 0 disk nbd3 43:96 0 0B 0 disk nbd4 43:128 0 0B 0 disk nbd5 43:160 0 0B 0 disk nbd6 43:192 0 0B 0 disk nbd7 43:224 0 0B 0 disk vda 254:0 0 1.8T 0 disk `-vda1 254:1 0 1.8T 0 part /etc/hosts /etc/hostname /etc/resolv.conf vdb 254:16 0 606.5M 1 disk nbd8 43:256 0 0B 0 disk nbd9 43:288 0 0B 0 disk nbd10 43:320 0 0B 0 disk nbd11 43:352 0 0B 0 disk nbd12 43:384 0 0B 0 disk nbd13 43:416 0 0B 0 disk nbd14 43:448 0 0B 0 disk nbd15 43:480 0 0B 0 diskThe NAME column maps to device paths such as /dev/loop1, while SIZE and MOUNTPOINTS help identify the intended disk.
- Confirm the exact device intended for formatting using size and mountpoint information.
$ df -h /root Filesystem Size Used Avail Use% Mounted on overlay 1.8T 17G 1.7T 1% /
Formatting the wrong device such as /dev/vda instead of a loop device permanently destroys the existing filesystem and its data.
- Unmount the target partition if a mountpoint is listed in lsblk or df output.
$ sudo umount /dev/loop1
No output from umount indicates the partition is now detached.
- Verify that the target partition is no longer mounted.
$ lsblk /dev/loop1 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS loop1 7:1 0 300M 0 loop
- List available filesystem-specific mkfs helpers on the system.
$ ls /sbin/mkfs.* /sbin/mkfs.bfs /sbin/mkfs.cramfs /sbin/mkfs.ext2 /sbin/mkfs.ext3 /sbin/mkfs.ext4 /sbin/mkfs.minix
Typing mkfs. and pressing <TAB> twice in many shells shows the same helpers via completion.
- Format the target partition with the desired filesystem type and optional label.
$ sudo mkfs.ext4 -F -L data /dev/loop1 mke2fs 1.47.0 (5-Feb-2023) Discarding device blocks: 0/76800 done Creating filesystem with 76800 4k blocks and 76800 inodes Filesystem UUID: 71cbb0a9-ef27-4ce6-98e9-1f67d2fea5cf Superblock backups stored on blocks: 32768 Allocating group tables: 0/3 done Writing inode tables: 0/3 done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: 0/3 done
Running mkfs.ext4 on an existing partition erases all contained files and directories and cannot be undone.
- Check the new filesystem type and UUID for the formatted partition.
$ sudo blkid /dev/loop1 /dev/loop1: LABEL="data" UUID="71cbb0a9-ef27-4ce6-98e9-1f67d2fea5cf" BLOCK_SIZE="4096" TYPE="ext4"
The UUID value is suitable for use in /etc/fstab to configure persistent mounts.
- Create a temporary mountpoint directory for the new filesystem.
$ sudo mkdir -p /mnt/format-demo
- Mount the new filesystem on the temporary mountpoint.
$ sudo mount /dev/loop1 /mnt/format-demo
For permanent configuration, define a dedicated mountpoint and a corresponding entry in /etc/fstab as described in How to mount disks and partitions in Linux.
- Verify that the mounted filesystem is accessible and initially empty.
$ ls -A /mnt/format-demo lost+found $ df -h /mnt/format-demo Filesystem Size Used Avail Use% Mounted on /dev/loop1 265M 24K 244M 1% /mnt/format-demo
- Unmount the filesystem again if the temporary mount is no longer required.
$ sudo umount /mnt/format-demo
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
