Linux online course

Create and Format a File System in Linux with mkfs

Learn how to identify a Linux partition, create a file system with mkfs, verify its UUID and type, mount it, and configure persistent mounting safely.

A Linux file system is the on-disk format and collection of data structures used to store and retrieve files and directories. It records file contents plus metadata such as ownership, permissions, timestamps, and the disk locations containing each file.

Creating a file system is commonly called formatting. Formatting is destructive: it replaces the existing file-system structures on the selected target and can make existing data inaccessible. Back up any data you need before running mkfs.

Partitioning, formatting, and mounting are different

These three operations occur at different stages:

  • Partitioning divides a disk into defined regions, such as /dev/sdb1. A partition is a region of a physical disk that can hold a file system.
  • Creating a file system writes structures such as inodes, allocation metadata, and superblocks into a partition or other block device.
  • Mounting makes the file system accessible through a directory called a mount point, such as /mnt/data.

A file system is not automatically accessible just because it has been created. It must be mounted before applications can use its files through the directory tree.

Identify the target device safely

Linux represents disks and partitions as block devices, usually under /dev. Device names vary according to the hardware or virtualization environment.

Common Linux storage device naming

SATA or SCSI disk: disk /dev/sda; first partition /dev/sda1. Partition numbers follow the disk name.

Virtual disk: disk /dev/vda; first partition /dev/vda1. This naming is common with virtual machines.

NVMe disk: disk /dev/nvme0n1; first partition /dev/nvme0n1p1. The p separates the disk number from the partition number.

eMMC device: disk /dev/mmcblk0; first partition /dev/mmcblk0p1. Like NVMe, eMMC partition names include p.

Do not assume that the first disk is always /dev/sda. USB, SATA, NVMe, virtual, and removable devices can receive different names between boots or between machines.

Inspect disks, file systems, and mounts

lsblk -f

The -f option displays useful file-system information, including device names, file-system types, labels, UUIDs, and mount points. Compare the output with the intended disk's capacity and partition layout.

findmnt

findmnt lists file systems currently mounted in the directory tree. The target must not be mounted when you format it.

sudo blkid

blkid reports recognized file-system types, labels, and UUIDs. Use all three commands before formatting, not just the success message from a later command.

Confirm that the selected partition is not:

  • the active root file system or another operating-system file system;
  • mounted at a directory;
  • an active LVM physical volume, RAID member, encrypted container, or swap device;
  • being used by an application or another storage layer.

Partition a disk before formatting

A partition is commonly created before the file system. Partition tables describe where partitions begin and end; the file system occupies the selected partition after it is created.

fdisk is an interactive command-line utility for viewing and modifying MBR or GPT partition tables. parted is another partitioning utility and is useful for interactive or scripted workflows.

sudo fdisk /dev/sdb

For an unused disk, the conceptual fdisk workflow is:

  1. Select the correct whole disk, such as /dev/sdb, rather than guessing from its name.
  2. Create a new partition and choose its size and partition type as appropriate.
  3. Review the proposed partition table before committing it.
  4. Write the partition table to disk, then quit.

After the table changes, ask the kernel to reread it:

sudo partprobe /dev/sdb

Then confirm the resulting partition:

lsblk /dev/sdb

The new partition might be named /dev/sdb1. In normal use, mkfs should be run on that partition, not accidentally on the entire disk /dev/sdb. Formatting the whole disk may overwrite its partition table and all partitions.

Storage workflow: partition, format, mount, persist

Inspect: identify devices and current usage with lsblk -f, findmnt, and blkid; result: a confirmed target.

Partition: define a disk region with fdisk or parted; result: a partition such as /dev/sdb1.

Create file system: use mkfs; result: file-system metadata on the partition.

Verify: use lsblk -f, blkid, and file-system-specific tools; result: confirmed type, label, and UUID.

Mount: use mount at a mount point; result: files become accessible through the directory tree.

Configure persistent mount: add a UUID-based entry to /etc/fstab and test with mount -a; result: the file system can be mounted automatically.

mkfs command syntax

The generic syntax is:

sudo mkfs -t FILE_SYSTEM_TYPE DEVICE
  • -t FILE_SYSTEM_TYPE selects the format, such as ext3 or ext4.
  • DEVICE is the block device to format, normally a confirmed partition such as /dev/sdb1.
  • mkfs is a front end that invokes a file-system-specific program such as mkfs.ext4 or mkfs.xfs.

Creating a file system changes disk metadata, so elevated privileges are normally required. The exact options available depend on the selected file-system implementation.

mkfs-related command options and tools

mkfs -t TYPE DEVICE: generic file-system creation; verify the device because it is destructive.

mkfs.ext4: directly invokes the ext4 creation utility; use a confirmed target.

mkfs -c: checks for bad blocks before creating the file system; a full scan can take a long time.

mkfs.ext4 -L LABEL: assigns a readable label during ext4 creation; labels must be suitable for the file system.

blkid: displays detected type, label, and UUID; detection output should match the intended target.

lsblk -f: summarizes block devices and file-system information; device names alone are not proof of identity.

Create an ext file system

ext3 example

ext3 is an older journaling Linux file system. Journaling records intended metadata changes so recovery after a crash is generally more manageable than with a non-journaling format.

sudo mkfs -t ext3 /dev/sda3

This demonstrates the generic syntax and the device-target pattern. Run it only after confirming that /dev/sda3 is the intended partition and is not mounted or in use.

Modern ext4 usage

ext4 is the widely used modern successor to ext3 and is a common general-purpose choice on Linux.

sudo mkfs -t ext4 /dev/sdb1

The file-system-specific form is:

sudo mkfs.ext4 /dev/sdb1

A label makes a file system easier to recognize in inspection output:

sudo mkfs.ext4 -L data /dev/sdb1

Labels are descriptive identifiers; the file system also receives a UUID, which is normally more suitable for persistent mount configuration.

Understand mkfs output and metadata

Successful ext formatting output may report blocks, block groups, inode tables, superblocks, and a completion message. These are not merely progress details: they describe the initial layout of the new file system.

File-system metadata reported during ext formatting

Blocks: fixed-size units of file-system allocation. The number of blocks represents the allocatable storage layout, while block size affects allocation behavior and overhead.

Inodes: data structures containing file metadata and block-location information. Each file and directory consumes an inode, so the inode count established at ext creation places an approximate limit on how many objects the file system can hold.

Block groups: ext-family layout groups containing blocks, inode data, and related metadata. Grouping helps organize allocation and metadata.

Superblocks: core metadata describing the file system's structure and state. Some ext layouts maintain backup copies.

Block size: the fixed allocation unit used by the file system. It influences space efficiency, layout, and how much data is allocated for small files.

An inode records metadata such as ownership, permissions, timestamps, file type, and references to the blocks containing file data. It does not normally contain the file's content itself.

Check for bad blocks during creation

A bad block is a storage area that cannot reliably store or return data. The -c option requests a bad-block check before creation:

sudo mkfs -t ext4 -c /dev/sdb1

A full scan can take considerable time, especially on a large disk. It is a format-time scan, not a complete hardware-health assessment. SMART diagnostics, where supported, provide broader device-health information. A bad-block scan also does not make failing hardware reliable; errors should prompt backup and replacement planning.

Verify the result after formatting

Do not rely only on a zero exit status or the final line of mkfs output. Confirm that the expected partition was formatted:

lsblk -f /dev/sdb1
sudo blkid /dev/sdb1

Check that the detected type is ext4, the label is the expected value, and a UUID is present. For ext file systems, additional metadata can be inspected with:

sudo tune2fs -l /dev/sdb1

tune2fs is intended for ext-family file systems. It can display the file-system label, UUID, block size, inode count, mount counts, and other superblock information. Use inspection tools appropriate to the actual file-system type.

Mount the new file system

Formatting creates the file system but does not expose it in the directory tree. Create a mount point and mount the partition:

sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data

Verify the specific mount:

findmnt /mnt/data
df -h /mnt/data

findmnt confirms which device is mounted at the path. df reports space usage for the mounted file system. These checks help detect a mount-point mistake, such as examining a directory that is still on the root file system.

Configure a persistent mount with UUID

Manual mounts normally last until shutdown or unmounting. To mount the file system automatically, add an entry to /etc/fstab. A UUID is a unique file-system identifier and is more stable than a device name that may change with hardware discovery order.

sudo blkid /dev/sdb1
sudoedit /etc/fstab

Add an entry using the actual UUID:

UUID=the-filesystem-uuid /mnt/data ext4 defaults 0 2

The fields specify the UUID, mount point, file-system type, mount options, dump setting, and file-system check order. Ensure that /mnt/data exists and that the type matches the value reported by blkid.

Validate the configuration without rebooting:

sudo mount -a
findmnt /mnt/data

If mount -a reports an error, correct the entry before rebooting. A malformed /etc/fstab entry can prevent the intended file system from mounting during startup.

Troubleshooting

The wrong target may be selected

When the device name is uncertain or the device contains an unexpected file system, stop. Run lsblk -f and findmnt, then compare capacity, partition layout, labels, UUIDs, and mount points with the intended storage. Use the confirmed partition path rather than guessing from disk order.

mkfs reports that the device is busy

Check findmnt and lsblk -f. Also determine whether the device belongs to LVM, RAID, encryption, or swap. Unmount the intended file system only when it is safe, and do not force formatting of an active system-storage component.

The new partition does not appear

Review the partition table with fdisk or lsblk. If the kernel has not reread the table, run:

sudo partprobe /dev/sdb

Re-scan as appropriate, or safely reboot after confirming that no other storage changes are pending.

Mount reports an unknown or wrong file-system type

Run blkid on the partition and compare the detected type with the mount configuration. Confirm that the required file-system support tools and kernel support are installed. Reformat only if the target is confirmed and data loss is acceptable.

A persistent mount fails

Check the UUID with blkid, the mount-point directory, the file-system type, and the field order in /etc/fstab. Correct the UUID-based entry and run sudo mount -a successfully before rebooting.

The file system reports no space unexpectedly

Check block usage:

df -h /mnt/data

For ext file systems, also check inode usage:

df -i /mnt/data

If blocks are exhausted, remove unneeded data or increase capacity. If inodes are exhausted, the workload may contain too many small files for the file system's inode layout; a file system recreated with suitable inode density may be required.

Bad-block checking finds errors

Treat scan errors as a possible hardware-reliability problem. Back up accessible data and review SMART-capable health information when applicable. Do not rely solely on marking bad blocks; plan to replace failing storage.

Exam-relevant notes

  • mkfs creates a file system; it does not partition a disk or mount the result.
  • The generic syntax is mkfs -t FILE_SYSTEM_TYPE DEVICE.
  • mkfs.ext4 is the file-system-specific equivalent for ext4.
  • Formatting the selected target destroys existing data.
  • Use lsblk -f, findmnt, and blkid to verify identity, file-system metadata, and mount status.
  • Use UUIDs in /etc/fstab for stable persistent mounts, and test entries with mount -a.
  • In ext file systems, inode availability can limit the number of files even when ordinary block space remains.

Next steps

For related administration tasks, continue with Linux command-line topics, review how to show the full path of shell commands, and learn more about Bourne Again Shell Bash.