Linux Commands

Working with Linux MKFS Command

You can’t avoid creating the file systems in Linux especially when working with the data storage devices. A file system organizes the directories in a hierarchical form down to its subdirectories and files. The mkfs command helps build the file systems in Linux, mainly on hard drives or external drives like USB.

The command stands for “make file systems” and you can create the various types of file systems. We will cover what the file systems are and their different types and the use of mkfs to create a file system on a file image on the Linux system. Take a look at the following illustratios.

Understanding File Systems in Linux

As a rule of the thumb, if you are unsure about working with the file system commands like mkfs, don’t try them on your actual system. You may crash it by erasing all its contents, but we will present a safer way of doing it.

On that note, let’s understand what a file system is. Linux uses the file system structures to manage the data on the various devices. Besides, a file system manages how the data gets stored or retrieved. To create a file system, we use the mkfs command. In other operating systems like Windows, this is referred to as disk partitioning. We have the various file system types for Linux and you specify which system to create when using the mkfs command.

Types of File Systems

You can list the available file systems on your Linux system by running the mkfs command and then pressing the tab key twice, leaving no space after the mkfs.

Your result may differ from the following:

File systems use a feature known as journaling. The concept involves the file systems having a record of completed file writes that it saves in a journal. Similarly, it creates a journal of the pending write records that get updated when writing to the files. This helps because when something causes an abrupt disruption, a file system can repair the broken files by referring to the journal. However, not all the file systems, especially the old ones, support this concept.

From the previous image, we see the various types of file systems including ext2, ext3, msdos, fat, vfat, etc. The file systems have their differences and it’s good to know which system to use when creating your file system.

How to Create a Linux File System Using mkfs

The mkfs command can mess up your system if you are not careful. To avoid this, we first create an image file that allows us to separate our system from the image file. We use the dd command to create our image file for this case. Also, we specify the path to our files for dd to use. The path is dev/zero which discards the data written to its files. We name our image file as linuxhint.img. Our file system is 100 MB in size. Replace the values and names to match your case.

The command will be:

$ dd if=/dev/zero of=~/linuxhint.img bs=1M count=100

In this case, each block is represented by 1 for 1MB.

To verify the image file, run the following command:

$ ls -hl | grep linuxhint

To create an ext4 file system or any of your choice, the command will be:

$ mkfs.ext4 ~/linuxhint.img

That’s it! Our ext4 file system is created with a temporary set-up point. Create a mount point for the file system. For this, let’s create a new directory named linuxhint and use it as our mount point. We need to give the mount point permissions to a given user, in this case, your username.

$ sudo mkdir /mnt/linuxhint

$ sudo mount ~/linuxhint.img /mnt/linuxhint

$ sudo chown kyle:users /mnt/linuxhint

Your file system is good to go. You can navigate the mount point and try creating files to see if it works.

We succeeded in creating an ext4 file system using the mkfs command. You can delete the mount point once you are satisfied with your tasks.

Conclusion

Creating the file systems is easy using the mkfs command. You can create the various file system types depending on what you want with the file systems. This guide presented an example of creating an ext4 file system on an image file. Using an image file when testing the file system commands is recommended to avoid messing with your system. Hopefully, you now understand how to use the mkfs command in Linux.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.