Overview
The mkfs (make filesystem) command creates a filesystem on a block device such as a hard disk partition, USB drive, or other storage media. It's actually a front-end that calls filesystem-specific utilities (mkfs.ext4, mkfs.xfs, mkfs.vfat, etc.). Understanding mkfs is critical for system administration tasks like preparing new disks, formatting storage devices, and setting up filesystems with specific characteristics for performance or reliability requirements.
Syntax: mkfs [OPTIONS] [-t type] device
Warning: mkfs destroys all existing data on the target device. Always double-check the device name before running this command!
⚠️ CRITICAL SAFETY WARNING
mkfs is DESTRUCTIVE! It will completely erase all data on the specified device. There is NO UNDO.
- Always verify the device name with
lsblk or fdisk -l
- Double-check you're not formatting your system disk
- Unmount the device before formatting
- Back up any important data first
- Be especially careful with wildcards and variables
Example 1
Creating an ext4 Filesystem (Most Common)
# First, identify the device:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
├─sda1 8:1 0 512M 0 part /boot
└─sda2 8:2 0 238G 0 part /
sdb 8:16 1 14.9G 0 disk
# Create ext4 filesystem on USB drive:
$ sudo mkfs.ext4 /dev/sdb1
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 3907072 4k blocks and 977280 inodes
Filesystem UUID: 12345678-1234-1234-1234-123456789abc
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
# Verify the filesystem:
$ sudo blkid /dev/sdb1
/dev/sdb1: UUID="12345678-1234-1234-1234-123456789abc" TYPE="ext4"
What's Happening:
The most common use case: creating an ext4 filesystem on a partition. First, use lsblk to identify the correct device. Then mkfs.ext4 creates the filesystem, setting up superblocks, inode tables, and the journal. Ext4 is the default filesystem for most Linux distributions, offering good performance, reliability, and large file/partition support.
Note: Use mkfs.ext4 directly or mkfs -t ext4. Both work the same way.
Example 2
Formatting with Custom Label and Options
# Create filesystem with a label:
$ sudo mkfs.ext4 -L "BACKUP_DRIVE" /dev/sdb1
# Set custom parameters:
$ sudo mkfs.ext4 -L "DATA" -N 1000000 -m 1 /dev/sdb1
# -L: Set volume label
# -N: Number of inodes (1 million)
# -m: Reserved space percentage (1% instead of default 5%)
# Check the filesystem:
$ sudo tune2fs -l /dev/sdb1 | grep -i "volume\|block\|inode"
Filesystem volume name: DATA
Block count: 3907072
Reserved block count: 39070
Free blocks: 3823456
Block size: 4096
Inode count: 1000000
# Mount by label:
$ sudo mount LABEL=DATA /mnt/backup
What's Happening:
Labels make filesystems identifiable and allow mounting by name instead of device path. The -N option sets the number of inodes (useful if you'll have many small files). The -m option reduces reserved space (default 5% can waste space on large drives). These optimizations are important for specific use cases like backup drives or file servers.
Pro Tip: For large data drives (>1TB) used for big files, reduce reserved space to 1% or even 0% to reclaim storage.