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 a 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 makes file systems

The mkfs command makes file systems. On other operating systems, creating a file system is called layout. Whatever its name, it is the process that prepares a partition in order to store data. The partition needs a way to store files, yes. But you also need a mechanism to store the names and locations of these files, as well as their metadata such as file creation timestamp, modified file timestamp, file size, and so on. 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 have just told mkfs the device partition on which you want to create the file system and the type of file system you want. It's on the face of it. Behind the scenes, it's a little different. For some time now, 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" and then the name of the file system you want to create.

To see the file systems that mkfs can create, type "mkfs" and then double-click the tab key. 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 review them and describe them briefly. After a quick word about logging.

Logging is an important concept in file systems. The file system saves the pending journal entries. As each file is written, the log is updated and the pending write records are updated. This allows the file system to repair partially written and broken files that have occurred as a result of a catastrophic event, such as a power failure. Some older file systems do not support logging. Those who do not write less often on the disk because they do not need to update the log. They may be faster, but they are more likely to be damaged by 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, Ext. Ext2 was The successor of Ext. Ext2 is not a log file system.
ext3: C & # 39; was the successor of Ext2, and can be likened to Ext2 with logging, which protects your file system against data corruption caused by sudden power outages and failures.
ext4: Ext4 is the standard file system for the latest Linux distributions. It's a solid, proven and reliable file system. It has characteristics that reduce fragmentation of files and can be used with disks, partitions and larger files than ext3.
BFS: That's the Boot filesystem, designed for one job and only one: manage the files of 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 used this file system without logging 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 will 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-healing and fault-tolerant operating system. The MINIX file system was designed as a simplified version of the Unix file system. Perhaps if you are developing on a Linux computer and targeting a MINIX platform, you can use this file system. Or maybe you need compatibility with a MINIX computer for other reasons. The use cases of 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 removes the eight-character limit for filenames. File names up to 255 characters have become possible. The only reason you will 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 read-only applications of specialists, such as in boot processes of Linux computers. It is common to have a small transient file system loaded first in order to be able to initiate bootstrapping processes to prepare for mounting the "real" boot system.
MSDOS: The file system of the Microsoft Disk operating system. Published in 1981, it is a basic file system as basic as possible. The first version did not even have directories. It occupies a prominent place in computing, but beyond compatibility with existing systems, there is little reason to use it today.

RELATED, RELATED, RELATED: Which Linux file system should you use?

A sure way to try file systems

Creating a file system on a partition destroys data that may already reside on that partition. The use of an available hard drive, or even an available computer, is the perfect way to create and use different file systems. But of course, many people do not have spare gear hanging around waiting 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 putting it into an image. We need to tell where to get his source data. We will use the if (input file) option to tell dd to use / dev / zero as an input data source. It will be a stream of zeros.

The (output file) option 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 added. We use the bs (block size) option to request a block size of 1 MB and the count option to request 250 blocks. This will give us a 250 MB file system. When you run this command, adjust the number of blocks according to your needs and the available capacity of your Linux computer.

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

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

The file is created for us and dd indicates 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 and use Ext2, which is the oldest version of Ext that this implementation of mkfs can create. This is a file system that does not have logging. So do not store anything valuable in it without making backups elsewhere. We use the mkfs.ext2 variant of the mkfs command and 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 appears.

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

This is a temporary installation, so we will 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

Now we can mount our image file.

sudo mount ~ / howtogeek.img / mnt / geek

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

We must change the file property from 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 the single-letter extension from the ~ / Documents / Code directory to our new file system. Let's check that they have been copied.

ls

ls in a terminal window

The files were copied, so our file system was created, mounted, and used. Or we think. Let's check again. From our personal directory, we will unmount the file system. Note that there is only one "n" in disassemble.

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 inside our image file, and this has been unmounted.

cd / mnt / geek
ls

cd / mnt / geek in a terminal window

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

Further exploration

Now that the process is developed, it is easy to try another file system. We will use the MINIX file system this time. In our home directory, we can create a new file system in 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 wishing to ask you "if you are certain of it", the new file system is created over the old one. 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 move on to the new / mnt / geek file system 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, we 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 acting. The problem with some commands is that they are potentially destructive. The dilemma is how to practice using them without jeopardizing your system or your data.

You now have a simple method to create and try 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.