Are you wondering how to use the mkfs command on Linux for easy and simple file formatting? We’ve got you covered with this guide.
In the operating system, you often want to format or make a file system. In this process, the operating system allocates a specific portion in your second storage where it keeps the record of all the files and directories available in the system, their properties (file creation date, modified date, size of the file, attributes), specific location in the disk where they are stored. For creating a filesystem, the mkfs command can be used. It stands for the make file system. Here, we discuss the mkfs command and its various options.
How to Use mkfs Command on Linux
The usage of
mkfs is very simple and straightforward. The user specifies the specific partition where the file system is to be created, along with the file system’s name. Depending on the operating system,
mkfs or another command,
mke2fs performs the job behind the scene. The specific syntax for
mkfs is as follows:
$ mkfs [-V][-t fstype][fs-options]filesys[blocks]
Knowing the list of operating system
mkfs command works by specifying the command
mkfs and the specific file system to use. To know the list of available file systems, you must type the command and press the Tab key. You will see the list of the file system. Following are the list of the operating system:
Ext2, Ext3, Ext4, BFS, FAT, NTFS, MINIX, VFAT, CRAMFS, and MSDOS.
Creating an image file for experimenting with the mkfs command
One can directly work with mkfs on the actual disk. However, this may lead to destructive effects as you may accidentally overwrite your important data. Another approach is to try the filesystem options on a spare hardware. For instance, the following command creates a filesystem on a floppy disk:
$mkfs -t ext2/dev/fd0
However, not many people has an extra partition or spare hardware. The better way is to create an image file. You may be able to work the same way as you are working on a disk or a partition once it is mounted.
For creating an image, we will use the
dd command. You should provide the source data to the
dd command. It will take the source data and put it in the image file. The following command creates an image file:
$dd if=/dev/zero of=~/data.img bs=1M count=250
The above command uses the if to tell the
dd to read the input stream as a stream of zeros. Using of option, we specify the name of the image file. Using the bs option, we request for a block of size 1MB, and the count specifies the number of such blocks.
Checking if the image file is created
Once the above command is executed, an image file will be created. Using the following command, we can check if the image file is created:
Creating a file system on the image file
Once the image file is created, we can create a file system on the image. The following command shows how we can create a file system:
Here, we have used the variant of
mkfs to create an
ext2 file system over the image file
Mounting the file system to a specific directory
Now, a file system has been created and acts virtually as a physical hard drive. You can mount the file system using the following set of commands. First, create a directory:
$sudo mkdir /mnt/data
And then mount the filesystem to the directory as follows:
$sudo mount ~/data.img/mnt/data
Before using the filesystem, appropriate permissions are to be granted for the mount point. The following command assigns the read and writes permissions:
$sudo chown rahul:users /mnt/data/
Using the new file system
Now, the file system is ready for use. You can change the directory to a new file system and performs various file system operations. Let’s change the current directory as follows:
Now, copy some files into it as follows:
You can perform all sorts of file operations, such as copy, rename, remove, etc., on the file system.
Unmount the filesystem
Once you have completed all the tasks, you can unmount the file system as follows:
After unmounting, if you try to look at the files in /mnt/data, you will not find it. Because, all the files are stored inside the image file.
Working with other filesystems
You can create other filesystems with the same procedure. The following command creates a file system:
Let’s mount the filesystem:
$sudo mount ~/data.img/mnt/data
Now, you can change the permission and copy, paste, rename, and remove the files.
Removing the mount point
Once you have completed your tasks, you can remove the mount point with the following command:
$sudo rmdir data
Options of mkfs
There are several other options of
mkfs, such as explicitly specifying a file system using -t option. If no option is specified, the default
ext2 filesystem is used. The
-V option can be used to verbose the output. The
-v option can be used for version information. Finally,
-h can be used for displaying the help of the command.
To check for bad blocks on the file system, the following command can be used:
$sudo mkfs -c /mnt/data
To check for the file system, the following command can be used:
$sudo file -sL/device
mkfs command returns an exit status of
0 on success and
1 on failure
We have discussed an overview of the
mkfs command. We have seen how we can create an image file, the filesystem, mount a filesystem, and create, copy, and update files. Using the image file, we can experiment with the command without touching any partition of your system.
If this guide helped you, please share it.