How to Use the mkfs Command on Linux

A Linux terminal on a laptop with an Ubuntu style desktop.Fatmawati Achmad Zaenuri / Shutterstock

You must create a file system before you can use any data storage device connected to a Linux computer. Learn how to use mkfs and other utilities to do just that for all sorts of file systems. We show you how.

mkfs creates file systems

The mkfs command creates file systems. On other operating systems, the creation of a file system is called formatting. Whatever its name, it is the process that prepares a partition for it to store data. The partition needs a way to store files, yes. But it also needs a mechanism to store the names and locations of these files, as well as their metadata such as file creation timestamp, modified timestamp of the file, the size of the file. file, etc. Once mkfs has created the framework for managing and storing file metadata, you can start adding files to the partition.

The syntax is very simple. You simply tell mkfs the device partition on which you want to create the file system and the type of file system you want. This is at first sight. Behind the scenes, it's a little different. For some time, on most Linux distributions, mkfs is a packaging for mke2fs. The mkfs command calls the mke2fs command and passes it the options you specified. Poor old mke2fs do all the work but does not get anything from the glory.

The mkfs syntax has been updated and the old format is deprecated. Both forms will work, but we will use the modern style in this article.

The choice of file systems

The modern way to use mkfs is to type "mkfs". Then the name of the file system you want to create.

To see the file systems that mkfs can create, type "mkfs" and then press the Tab key twice. There is no space after "mkfs", simply press Tab twice.

List of supported file systems in a terminal window

The list of available file systems is displayed in the terminal window. The screen capture comes from Ubuntu 18.04 LTS. Other distributions may offer more or less options. We will go through them and describe them briefly. After a short note on the journaling.

Logging is an important concept in file systems. File systems record pending file writes to a log. As each file is written, the log is updated and the pending write records are updated. This allows the file system to repair broken and partially written files that have occurred due to a catastrophic event such as a power outage. Some of the older file systems do not support logging. Those who do not write to the disk less frequently because they do not need to update the log. They can run faster, but they are more prone to damage from broken file writes.

ext2: The very first file system for Linux was the MINIX file system. It was then replaced by the first file system ever written specifically for Linux, which was Ext. Ext2 was Successor of Ext. Ext2 is not a log file system.
ext3: This was the successor of Ext2and can be considered Ext2 with logging, which protects your file system against data corruption caused by sudden crashes and power outages.
ext4: Ext4 is the standard file system for May Linux distributions. It is a solid, proven and reliable file system. It has features that reduce fragmentation of files and can be used with larger disks, partitions, and files than Ext3.
BFS: That's the Boot file system, which is designed for one job and only one: to manage the files in the boot partition. It is rare for you to create a boot file system by hand. Your Linux installation process will do it for you.
FAT: The File Allocation Table The file system was designed for floppy disks by a consortium of heavyweights in the computer industry. It was introduced in 1977. The only reason you would use this non-journaled file system is for compatibility with non-Linux operating systems.
NTFS: The New technology file system is a Microsoft Journaling File System introduced with Windows NT. It was the successor of FAT. The only reason you would use this file system is compatibility with non-Linux operating systems.
MINIX: Originally created by Andrew S. Tanenbaum as a teaching assistant, MINIX is a "mini-Unix" operating system. Today, it aims to provide a self-repairing and fault-tolerant operating system. The MINIX file system was designed simplified version of the Unix file system. Maybe if you cross-develop on a Linux computer and target a MINIX platform, you can use this file system. Or maybe you need compatibility with a MINIX computer for other reasons. The cases of using this file system on a Linux computer do not jump to my eyes, but they are available.
VFAT: Virtual file allocation table, was introduced with Windows 95 and removed the eight-character limit for filenames. Filenames containing up to 255 characters have become possible. The only reason you would use this file system is compatibility with non-Linux operating systems.
CRAMFS: The Compressed ROM file system is a read-only file system designed for embedded systems and specialized read-only applications, as well as in boot processes for Linux computers. It is common to first load a small transient file system so that bootstrapping processes can be started to prepare the "real" boot system to mount.
MSDOS: The file system of the Microsoft Disk operating system. Released in 1981, it is an elementary file system as basic as possible. The first version did not even have directories. It occupies a prominent place in the history of computer science but, beyond compatibility with older systems, there is no reason to use it today. ; hui.

RELATED: Which Linux file system should you use?

A sure way to experiment with file systems

Creating a file system on a partition is destructive for any data that might already reside on that partition. Using a spare hard drive – or even a spare computer – is the perfect way to experiment with creating and using different file systems. But of course, many people do not have spare equipment that expects to be experienced.

However, we can create an image file and create file systems inside it. Once we have mounted it, we can use it as it was a normal partition. We can explore and experiment with file systems without the need for replacement hardware. We will use the dd command for create our image file.

The image file is created by taking the source data and inserting it into an image. We need to tell where to get his source data. We will use the option if (input file) to tell dd to use / dev / zero as a source of input data. It will be a stream of zeros.

The option of (output file) allows us to give a name to the image file. We will call it "howtogeek.img".

The size of the image file is determined by the size and number of blocks we add to it. We use the option bs (block size) to request a block size of 1 MB and the count option to request 250 blocks. This will give us a file system of 250 MB. When you run this command, adjust the number of blocks according to your needs and the available capacity that you have on your Linux computer.

dd if = / dev / zero of = ~ / howtogeek.img bs = 1M count = 250

dd if = / dev / zero of = ~ / howtogeek.img bs = 1M count = 250

The file is created for us and dd reports that 250 blocks have been created for us as requested.

Output of the dd command in a terminal window

We can look at our image file with ls:

ls -hl

ls -hl in a terminal window

It's 250 MB as expected, which is encouraging.

Creating the file system

Let's choose a file system to use. We will go back in time and use Ext2, which is the first version of Ext that this implementation of mkfs can create. It is a file system without logging, so do not store anything valuable without having backups elsewhere. We use the mkfs.ext2 variant of the mkfs command, and we tell it to use our image file as a target.

mkfs.ext2 ~ / howtogeek.img

mkfs.ext2 ~ / howtogeek.img in a terminal window

The file system is created and some details of the file system are displayed.

Output of the mkfs.ext2 command in a terminal window

As you can see on the highlighted text, mke2fs makes an appearance.

We now have a container for the file system – the image file – that replaces a hard disk in this scenario. Inside this container, we created a file system. We must now mount the file system in order to use it.

This is a temporary configuration, so let's make a mount point in / mnt called "geek". We will delete it when we are done.

sudo mkdir / mnt / geek

mkfs.ext2 ~ / howtogeek.img in a terminal window

We can now mount our image file.

sudo mount ~ / howtogeek.img / mnt / geek

mkfs.ext2 ~ / howtogeek.img in a terminal window

We must change the file property of the mount point so that we have read and write access to it.

sudo chown dave: users / mnt / geek /

sudo chown dave: users / mnt / geek / in a terminal window

And now, we should be able to use our new file system. Let's go to the file system and copy some files to it.

cd / mnt / geek
cp ~ / Documents / Code / *.? .

cp ~ / Documents / Code / *.? . in a terminal window

This will copy all files with a single letter extension from the ~ / Documents / Code directory to our new file system. Let's check that they have been copied.


ls in a terminal window

The files were copied, so our file system was created, mounted, and used. Or at least we think so. Recheck. From our personal directory, we will unmount the file system. Note that there is only one "n" in umount.

sudo umount / mnt / geek

sudo umount / mnt / geek in a terminal window

Now, if we go back to / mnt / geek and look for files, we should not find any because they are in our image file, and this has been unmounted.

cd / mnt / geek

cd / mnt / geek in a terminal window

RELATED: How to mount and unmount storage devices from the Linux terminal

In-depth exploration

Now that the process is developed, trying another file system should be easy. We will use the MINIX file system this time. In our home directory, we can create a new file system inside our existing image file.

Pay attention! If there are important files on the file system inside the image file, mount the image file and recover them before creating a new file system.

mkfs.minix ~ / howtogeek.image

mkfs.minix ~ / howtogeek.image in a terminal window

Without any clue to ask you "if you are sure," the new file system is created on the old. We can mount our image file with exactly the same command as before:

sudo mount ~ / howtogeek.img / mnt / geek

sudo mount ~ / howtogeek.img / mnt / geek in a terminal window

Let's go to the new file system in / mnt / geek and see if we can create a file.

touch geek.txt

touch geek.txt in a terminal window

ls -ahl geek.txt

ls -ahl geek.txt in a terminal window

And, as simply and as fast as that, we have created a new file system, have it mounted and we can use it.

Removing the mount point

When you're done, we can remove the "geek" mount point. To do this we will use rmdir:

cd / mnt
sudo rmdir geek

sudo rmdir geek in a terminal window

Juggle with the fire

With Linux, as with most things, you learn by doing. The problem with some commands is that they are potentially destructive. The dilemma is how to use them without putting your system or data at risk?

You now have a simple method for creating and trying file systems with mkfs that leaves your computer intact.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.