On Unix-like operating systems, the mkfs command creates and builds a file system.
This page describes the Linux version of mkfs.
- Related commands
- Linux commands help
mkfs is used to build a Linux file system on a device, usually a hard disk partition. The device argument is either the device name (e.g., /dev/hda1, /dev/sdb2), or a regular file containing the file system. The size argument is the number of blocks to be used for the file system.
The exit status returned by mkfs is 0 on success and 1 on failure.
In actuality, mkfs is a front-end for the various file system builders (mkfs.fstype) available under Linux. The filesystem-specific builder is searched for in many directories, like perhaps /sbin, /sbin/fs, /sbin/fs.d, /etc/fs, /etc (the precise list is defined at compile time, but at least contains /sbin and /sbin/fs), and finally in the directories listed in the PATH environment variable. Please see your filesystem-specific builder manual pages for further details.
|-t, –type type||Specify the type of file system to be built. If not specified, the default file system type (currently ext2) is used.|
|fs-options||Filesystem-specific options to be passed to the real file system builder. Although not guaranteed, the following options are supported by most file system builders.|
|-V, –verbose||Produce verbose output, including all filesystem-specific commands that are executed. Specifying this option more than once inhibits execution of any filesystem-specific commands. This is really only useful for testing.|
|-V, –version||Display version information and exit. Option -V displays version information only when it is the only parameter, otherwise it works as –verbose.|
|-h, –help||Display help and exit.|
The example above would create an ext2 file system on a floppy diskette in the first floppy drive.
fdisk — A disk partitioning utility.
fsck — Check and repair a Linux file system.
losetup — Set up and control loop devices.
mkfs is the command line tool in Linux to format a disk or partition in a certain filesystem of your choice.
Table of Contents
The letters in mkfs stand for “make file system”. The command is commonly used for managing storage devices in Linux. You can think of mkfs as a command line tool for formatting a disk into a specific filesystem.
You can also use the mkfs command in the following fashion.
In this tutorial, I will discuss generally what a file system is. I’ll also provide examples for using the mkfs command effectively.
Since the mkfs command deals with disks and partitions, it requires you to be either root or sudo user with access to run admin commands.
mkfs is a powerful tool. It is important to understand the potential consequences of altering the filesystem. Selecting the wrong device node will erase all data on that device. Use this command at your own risk. You have been warned
What is a file system, again?
A file system (fs) refers to the structure and logic that manage data on a device. The file system controls how data is stored and retrieved.
There are many types of file systems and each has its own advantages and disadvantages. Here are the most common type of filesystems:
You have probably come across one or more of these filesystem types before. You may even associate the types with their respective operating systems.
Generally speaking, FAT/NTFS are designed for Windows, Ext is used with Linux systems, and APFS/HFS are macOS file systems. Each of these address the logic of file structure differently which can result in issues.
This is why it is crucial to think about this before declaring a filesystem type, or “formatting” your device. Each use case is different, and it is up to you to decide what filesystem works best for your needs.
mkfs command examples
Let me show you some of the most common usage of the mkfs command.
Please don’t try any of these commands on your actual system blindly. It will format the target device/disk partition erasing all the data.
mkfs without any option creates ext2 filesystem
If you don’t specify the filesystem type for the target device, it will use the default ext2 filesystem.
Specify filetype with mkfs
Using ext2 is not very convenient or advisable. Most Linux systems these days recommend using ext4. You can specify the file system in the following manner:
You can also use it like this:
Check for bad blocks on a device
You can also check for bad blocks on a device but keep in mind that the check often takes a long time.
Bonus tip: Check the filesystem type of a device
You may want to check the type of filesystem of a device that you just formatted using mkfs command. You can use the file command for this purpose.
Here’s an example that shows what kind of output it could show:
Real life use case: Using mkfs command to create a File System on a USB device
Now that you have some background information, you can start using mkfs.
The most practical demonstration I can think of is formatting a USB flash storage drive. These same principles will apply to any type of storage you choose.
Find Your Device
First you will need to find your device. One method you can use is sudo fdisk -l . This will list all disk nodes that are currently mounted.
Your output will obviously vary. Please be very careful when identifying your desired drive. If you are unsure, remove the disk and run the fdisk -l command again. If you have the correct device, it won’t be listed while disconnected.
Verify the Partition
The device I’m using is a Patriot Memory USB and it is located at /dev/sdb . In addition to identifying the correct disk, you will need to make sure that you are changing the fs of the desired partition.
I used fdisk tools to delete existing data and write a new partition table. While I was doing that, I created a new partition to write to. That partition will be our target: /dev/sdb1 .
Before you attempt to change the file system, you will need to unmount it using the umount command.
Create the File System
Now that you have verified your target and unmounted the drive, you can proceed to create the file system.
I have added the -v verbose option here to display more information when running.
This process can take some time, but should finish in under 20 minutes unless the target is larger than 2 TB.
I had several issues with the program hanging on the last item. Unfortunately, there is no progress indicator and I had no errors thrown.
Verify the newly created filesystem
First, check the filesystem of the device you just used. Is it the one you wanted?
It is important to make sure that the device is recognized on the systems you will use it with. I created a folder called test and a file within it called test.txt.
To save time, you can copy and paste my commands here.
If everything worked, you should be able to mount the drive to your desired systems and access the files. If you cannot access the files on your system, there is probably a compatibility issue.
I hope you find this quick guide to mkfs command useful. If you like this tutorial, please share it on social media.
If you have any comments or questions, please leave them below. If you have any suggestions for topics you’d like to see covered, feel free to leave those as well.