mkfs Command in Linux with Examples
Last Updated :
28 Oct, 2024
The mkfs command stands for “make file system” and is utilized to create a file system, which organizes a hierarchy of directories, subdirectories, and files, on a formatted storage device. This can be a partition on a hard disk drive (HDD), a USB drive, or other storage media. A partition is a logically independent part of an HDD, and creating a file system involves formatting it to organize the data.
Syntax
mkfs [ -V ] [ -t fstype ] [ fs-options ] filesys [ blocks ]
Key Options with mkfs Command
- filesys: The name of a device file (e.g., /dev/hda3 for the third partition on the primary HDD) or the mount point for the new file system.
- -t fstype: Specifies the type of file system to be created (default is ext2 if not specified).
- -V: Produces verbose output, including all file system-specific commands executed.
- -c: Checks the storage device for bad blocks before creating the file system.
- -l: Reads the bad blocks list from a specified file.
Example
To create a file system on a partition, you can run the following command:
sudo mkfs.ext4 /dev/sdb1
This command creates an ext4 file system on the specified partition (/dev/sdb1).
Journaling
It is a significant idea in file systems. The file system records the awaiting file keeps in touch with a diary. As each file is composed of, the diary is refreshed, and the unresolved setup accounts are refreshed. This permits the file system to fix broken, halfway composed files that have happened because of a disastrous occasion, for example, a power cut.
A portion of the more seasoned file systems doesn't uphold journaling. Those that don't, keep in touch with the disk, less regularly in light of the fact that they don't have to refresh the diary. They might give a faster performance, yet they are more inclined to harm because of interrupted file writes.
- In the modern era, the way of utilizing mkfs is to type “mkfs.” and then the name of the file system you wish to create.
- Now, in order to see all the file systems offered by the "mkfs" command, hit the Tab key twice.
- The list of all available file systems available in the Linux distribution being used will be displayed in the terminal window. The screenshot is from Ubuntu 18.04 LTS. Other distributions may have more or fewer options:

Making a File System on a USB
1. Finding the required device on the OS through the terminal.
Type in the following command, and it will show all the disk nodes that are currently mounted. Be always sure in choosing the right disk or otherwise, you can remove the storage device and then again plug it in if the above command is not showing it in the list, thereafter again run the above command to list all the nodes.
Here, our required disk is "/dev/sdb" which is of 3.7 GiB.
sudo fdisk -l


2. Un-mounting the USB drive's partition
Un-mounting the storage drive is necessary before performing the format. Run the following command, but remember to replace “sdb1" with your USB drive's partition label, and then press Enter.
sudo umount /dev/sdb1

3. Erasing all the data on the drive (discretionary)
You can erase everything on the drive by entering the following command. But, remember to supplant “sdb” with your USB drive's name.
sudo dd if=/dev/zero of=/dev/sdb bs=4k status=progress && sync

4. Creating a new partition table
Type in the following command, by replacing “sdb” with your USB drive's label, and then press Enter.
sudo fdisk /dev/sdb

Enter “o” for creating an empty partition table.

Enter the option “n” for creating a new partition.

Enter “w” to write the table and exit.

5. Execute the following command for viewing the partition.
lsblk

6. Formatting the new volume created
Enter the following command and press Enter for formatting the drive as ext4. Remember to, replace "sdb1" with your partition's label:
sudo mkfs.vfat /dev/sdb1

7. Verifying the newly created filesystem
Run the following command in the terminal:
sudo file -sL /dev/sdb1

8. Lastly, execute the following command for ejecting the drive when finished.
sudo eject /dev/sdb

We have successfully formatted the USB storage device and have also created a file system with a partition.
Conclusion
The mkfs command is essential for creating file systems on various storage devices in Linux. Understanding its syntax and options allows users to format storage media effectively and ensure data organization.
Similar Reads
ln command in Linux with Examples The 'ln' command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you're unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use ca
3 min read
Linux make Command with Examples The make command for Linux is a very useful utility in the automation of software development and performing tasks in a Linux environment. It simply reads a special file, which is called a Makefile and this file describes how one's program is compiled and linked with another file or another program
6 min read
uname command in Linux with Examples Linux, renowned for its open-source flexibility and powerful performance, offers a range of commands that reveal the inner workings of your system. Among these, the 'uname' command stands out as a versatile tool that provides key details about your Linux machine. Here, we will learn the basics of th
4 min read
rcp Command in Linux with examples When working in a Linux environment, there often comes a time when you need to transfer files from one computer to another. While more secure options like scp or rsync exist, the rcp (Remote Copy Protocol) command offers a simple and efficient way to copy files between systems, especially for beginn
5 min read
rmdir Command in Linux With Examples In Linux, managing directories efficiently is a crucial skill for both system administrators and everyday users. The 'rmdir' command is a specialized tool designed specifically for removing empty directories, helping to maintain a clean file system and avoid accidental data loss. This guide delves i
6 min read
tty Command in Linux with Examples In the Linux operating system, everything is represented within a file system, including hardware devices and terminal interfaces. Among these, the terminal is also treated as a file, allowing users to interact with the system by sending inputs and receiving outputs. One essential command related to
3 min read