mkfs Command in Linux

Introduction

In this tutorial, we will delve into the Linux mkfs command, a crucial tool for system administration that enables you to create file systems on partitions or entire storage devices. The mkfs command empowers you to format storage media with diverse file system types, including ext4, FAT32, and NTFS. We will begin with an overview of the mkfs command and its supported file system options, then move on to practical exercises such as creating a file system on a partition and formatting a USB drive using the mkfs command.

Introduction to the mkfs Command

This section introduces the mkfs command in Linux, a fundamental utility for systemadmin tasks related to creating file systems on partitions or storage devices. The mkfs command is a powerful asset that allows you to format storage media with a range of file system choices, such as ext4, FAT32, and NTFS.

To start, let's determine the file system types supported on your system:

sudo mkfs.types

Example output:

Filesystem types supported:
    ext2
    ext3
    ext4
    fat
    minix
    msdos
    ntfs
    vfat

The mkfs command operates as a front-end to file system-specific utilities like mkfs.ext4, mkfs.fat, and mkfs.ntfs. You can specify the desired file system type using the -t option with the mkfs command.

For instance, to create an ext4 file system on a partition, you would use the following command:

sudo mkfs -t ext4 /dev/sdb1

This command will format the /dev/sdb1 partition with the ext4 file system.

In the subsequent section, we will learn the procedure of creating a file system on a partition using the mkfs command.

Creating a Filesystem on a Partition

This section guides you through the process of creating a file system on a partition using the mkfs command.

Initially, we need a partition on a storage device. For this example, we will use a USB drive. Connect a USB drive to your system and execute the following command to display the available block devices:

sudo fdisk -l

Example output:

Disk /dev/sdb: 14.9 GiB, 16008609792 bytes, 31266176 sectors
Disk model: USB Drive
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9a3d4d3b

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        2048 31266175 31264128 14.9G 83 Linux

Based on the output, the USB drive is identified as /dev/sdb and contains a single partition, /dev/sdb1.

Now, let's create an ext4 file system on the partition:

sudo mkfs -t ext4 /dev/sdb1

Example output:

mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 3908016 4k blocks and 976768 inodes
Filesystem UUID: 5d0d4d5e-d4d2-4d2d-9d2d-d4d2d4d2d4d2
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

This command formats the /dev/sdb1 partition with an ext4 file system.

The next section will guide you on how to format a USB drive utilizing the mkfs command.

Formatting a USB Drive with the mkfs Command

This section focuses on formatting a USB drive using the mkfs command.

The first step is to identify the USB drive's device name. Employ the following command to list the recognized block devices:

sudo fdisk -l

Example output:

Disk /dev/sdb: 14.9 GiB, 16008609792 bytes, 31266176 sectors
Disk model: USB Drive
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9a3d4d3b

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        2048 31266175 31264128 14.9G 83 Linux

According to the output, the USB drive is designated as /dev/sdb.

Let's format the USB drive with the FAT32 file system now:

sudo mkfs -t vfat /dev/sdb

Example output:

mkfs.fat 4.2 (2021-01-31)
/dev/sdb: 14.9 GiB, 16008609792 bytes, 31266176 clusters
FAT type is FAT32, cluster size is 4096 bytes
Root directory entries is 0
sectors per FAT is 1960
Media byte is 0xf8
Sectors per track is 64
Number of heads is 128
Hidden sectors is 2048
Total sectors is 31266176
File system type is FAT32

This action will format the entire /dev/sdb USB drive with a FAT32 file system. Note that running this on the device (sdb instead of sdb1) will erase any partition table. Use with caution.

The subsequent step involves mounting the formatted USB drive and accessing its contents.

Summary

This tutorial provided insight into the mkfs command in Linux, which system administrators use to create file systems on partitions or storage devices. We explored the available file system types on the system and demonstrated how to use the mkfs command to create an ext4 file system on a partition. We also covered formatting a USB drive using the mkfs command.

This hands-on tutorial has given you a practical understanding of the mkfs command and its role in formatting storage media with a variety of file system types. This knowledge is critical for effective file system management and maintenance on Linux systems, especially when working as root or a systemadmin.

400+ Linux Commands