Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Disks and Filesystems

Linux mkfs command

Reference for the Linux mkfs command and mkfs.ext4, mkfs.xfs and mkfs.vfat: format a partition, set labels and options, and recognize filesystems by their magic numbers.

mkfs ("make filesystem") writes an empty filesystem onto a block device or a file: it lays down the structures a filesystem needs (superblock, inode tables, allocation maps, a journal) so that the device can be mounted and used. It is the step between partitioning a disk and mounting it.

mkfs is only a front end. mkfs -t ext4 /dev/sdb1 runs the real tool mkfs.ext4 (also known as mke2fs); there are matching programs such as mkfs.xfs, mkfs.btrfs, mkfs.vfat and mkfs.ntfs. Formatting erases the existing filesystem. The tools are not available in our sandbox, so the commands are read-only; the live examples use a plain file to show how filesystems are recognized and how sizes work out.

Terminal
sudo mkfs -t ext4 -L data /dev/sdb1     # read-only example
sudo mkfs.xfs /dev/sdc1

Try it

Bash Recognizing filesystems by magic numbers

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.

Bash How big will the filesystem be?

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

Terminal
mkfs [-t TYPE] [FS-OPTIONS] DEVICE [BLOCKS]
mkfs.ext4 [-b BLOCKSIZE] [-L LABEL] [-m RESERVED%] [-i BYTES-PER-INODE] [-N INODES] [-U UUID] [-F] DEVICE
Name Description
-t TYPE Filesystem type; runs mkfs.TYPE. Defaults to ext2 if omitted.
DEVICE A partition (/dev/sdb1), LVM volume or a regular file.
BLOCKS Optional size in blocks. Defaults to the whole device.
-L LABEL (ext4, xfs) / -n NAME (vfat) Volume label, handy for mount LABEL=....
-b SIZE (ext4) Block size in bytes: 1024, 2048 or 4096 (the usual default).
-m PERCENT (ext4) Percentage of blocks reserved for root.
-i RATIO / -N COUNT (ext4) Bytes per inode, or an exact number of inodes.
-F Format a regular file or force a mounted device (ext4). Dangerous on a real device.
-f (xfs, btrfs) Overwrite an existing filesystem.
-q Quiet.

Returns: 0 on success, non-zero if the device is busy, missing, too small, or the helper fails. If the device holds an existing filesystem, most tools ask for confirmation or refuse without -f.

Usage notes

  • It erases. The old filesystem is gone and its data is usually unrecoverable in practice. Check the device name twice with lsblk, and never run mkfs on a mounted device.
  • Format partitions, not the table. The right order is partition (fdisk and parted), then mkfs, then mount, then add the UUID to /etc/fstab. Get the UUID from blkid.
  • Choosing a type. ext4 is the safe default; xfs handles large files and parallel I/O well but cannot be shrunk; btrfs and zfs add snapshots and checksums; vfat/exfat are for compatibility with Windows, macOS and cameras; ntfs for Windows disks.
  • Reserved space. For data disks that no system service writes to, -m 0 or -m 1 returns space that ext4 holds back for root by default. Check with df.
  • Inodes are fixed at creation on ext4 (not on XFS, which allocates them dynamically). If you expect millions of small files, set -i or -N now.
  • Practice on files. truncate -s 200M test.img && mkfs.ext4 -F test.img && sudo mount -o loop test.img /mnt costs nothing and teaches the whole workflow. See truncate.
  • Quick versus full. Modern tools create the filesystem quickly without zeroing every block. Features such as lazy_itable_init let ext4 finish its initialization in the background after the first mount.
  • Unix permissions on FAT. vfat/exfat have no owners or modes, so chmod and chown do not work; ownership comes from mount options (uid=, umask=).