Linux mkfs Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux mkfs command, which is used to create file systems on partitions or storage devices. The mkfs command allows you to format storage media with various file system types, such as ext4, FAT32, and NTFS. We will start by learning about the mkfs command and its available file system types, then proceed to create a file system on a partition and format a USB drive using the mkfs command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/sudo -.-> lab-422807{{"`Linux mkfs Command with Practical Examples`"}} linux/dd -.-> lab-422807{{"`Linux mkfs Command with Practical Examples`"}} linux/mount -.-> lab-422807{{"`Linux mkfs Command with Practical Examples`"}} end

Introduction to the mkfs Command

In this step, we will learn about the mkfs command in Linux, which is used to create file systems on partitions or storage devices. The mkfs command is a powerful tool that allows you to format storage media with various file system types, such as ext4, FAT32, and NTFS.

First, let's check the available file system types on your system:

sudo mkfs.types

Example output:

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

The mkfs command is a wrapper around various file system-specific commands, such as mkfs.ext4, mkfs.fat, and mkfs.ntfs. You can use the mkfs command with the -t option to specify the file system type you want to create.

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

sudo mkfs -t ext4 /dev/sdb1

This will create an ext4 file system on the /dev/sdb1 partition.

In the next step, we will learn how to use the mkfs command to create a file system on a partition.

Creating a Filesystem on a Partition

In this step, we will learn how to create a file system on a partition using the mkfs command.

First, let's create a partition on a storage device. In this example, we'll use a USB drive. Plug in a USB drive and run the following command to list 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

In the output, you can see that the USB drive is /dev/sdb and it has one 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 creates an ext4 file system on the /dev/sdb1 partition.

In the next step, we will learn how to format a USB drive with the mkfs command.

Formatting a USB Drive with the mkfs Command

In this step, we will learn how to format a USB drive using the mkfs command.

First, let's identify the USB drive device. Run the following command to list 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

In the output, you can see that the USB drive is /dev/sdb.

Now, let's format the USB drive with the FAT32 file system:

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 creates a FAT32 file system on the entire /dev/sdb USB drive.

In the next step, we will learn how to mount the formatted USB drive and access its contents.

Summary

In this lab, we learned about the mkfs command in Linux, which is used to create file systems on partitions or storage devices. We explored the available file system types on the system and how to use the mkfs command to create an ext4 file system on a partition. We also learned how to create a file system on a USB drive using the mkfs command.

The lab provided a practical understanding of the mkfs command and its usage in formatting storage media with various file system types. This knowledge can be applied to efficiently manage and maintain file systems on Linux systems.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like