mkfs Command in Linux with Examples

14 Oct, 2020
The mkfs command stands for “make file system” is utilized to make a file system (which is, a system for organizing a hierarchy of directories, subdirectories, and files) on a formatted storage device usually, a partition on a hard disk drive (HDD) or it can also be a USB drive, etc. A partition is logically an autonomous part of an HDD. An organized segment is one to which a low-level arrangement or format, additionally called a physical format(organization), has been applied. It comprises separating the disk’s tracks into a predetermined number of divisions and filling the information zone of every segment with dummy bytes. 
These outcomes in the demolition of any current information on the disk. Formatting(organizing) of new HDD and floppy plates is done at the manufacturing plant. It is seldom important to do a low-level format on an HDD. The creation of a file system is also known as high-level formatting or logical formatting. It includes making a table of contents for the partition or disk, but in this case, the data already present on the disk or segment is not destroyed. Basically, “mkfs” is just a front-end for the various specific file system creation programs that are available in Linux, such as mke2fs, mkfs.ext3 and mkfs.vfat, etc. When the “mkfs” command is compiled, then a precise list of standard directories is created, and therefore the specified program is searched for from the same list.
Syntax for mkfs command:
mkfs [ -V ] [ -t fstype ] [ fs-options ] filesys [ blocks ]

The items in the square brackets are discretionary, but the main obligatory argument is “filesys”. filesys is the name of a device document (i.e., a record that the system uses to execute admittance to a physical device), for example, /dev/hda3, the third segment on the primary HDD, or/dev/fd0, the principal floppy disk. It can likewise be the mount point (i.e., where it is joined to the system) for the new file system.
The most commonly used option is “-t”, which is utilized for specifying the type of file system to be created. If this option is not used, the default filesystem created will be ext2 (second extended file system) from the other types of file systems that can be created like ext3, minix, msdos, vfat and xfs.
The -V option is used to produce verbose output, and also includes all file system-specific commands that are executed. By specifying this option more than once, the execution of any file system-specific commands can be prevented.
The “-c” option, will check the storage device for bad blocks before creating the file system, and the “-l” option, will read the bad blocks list from a file whose name follows it.
“fs-options” stand for file system-specific options that are to be passed to the real file system creation program (i.e., the program for which mkfs is serving just as a front end). 
It is a significant idea in file systems. The file system records the awaiting file keeps in touch with a diary. As each file is composed of, the diary is refreshed, and the unresolved setup accounts are refreshed. This permits the file system to fix broken, halfway composed files that have happened because of a disastrous occasion, for example, a power cut. A portion of the more seasoned file systems doesn’t uphold journaling. Those that don’t, keep in touch with the disk, less regularly in light of the fact that they don’t have to refresh the diary. They might give a faster performance, yet they are more inclined to harm because of interrupted file writes.
In the modern era, the way of utilizing mkfs is to type “mkfs.” and then the name of the file system you wish to create.
Now, in order to see all the file systems offered by the “mkfs” command, hit the Tab key twice.
The list of all available file systems available in the Linux distribution being used will be displayed in the terminal window. The screenshot is from Ubuntu 18.04 LTS. Other distributions may have more or fewer options:
 
1. Finding the required device on the OS through the terminal. Type in the following command, and it will show all the disk nodes that are currently mounted. Be always sure in choosing the right disk or otherwise, you can remove the storage device and then again plug it in if the above command is not showing it in the list, thereafter again run the above command to list all the nodes. Here, our required disk is “/dev/sdb” which is of 3.7 GiB.
sudo fdisk -l

2. Un-mounting the USB drive’s partition
Un-mounting the storage drive is necessary before performing the format. Run the following command, but remember to replace “sdb1′′ with your USB drive’s partition label, and then press Enter. 
sudo umount /dev/sdb1

3. Erasing all the data on the drive (discretionary)
You can erase everything on the drive by entering the following command. But, remember to supplant “sdb” with your USB drive’s name.
sudo dd if=/dev/zero of=/dev/sdb bs=4k status=progress && sync

4. Creating a new partition table
Type in the following command, by replacing “sdb” with your USB drive’s label, and then press Enter.
sudo fdisk /dev/sdb

Enter “o” for creating an empty partition table.
Enter the option “n” for creating a new partition.
Enter “w” to write the table and exit.
5. Execute the following command for viewing the partition.
lsblk

6. Formatting the new volume created
Enter the following command and press Enter for formatting the drive as ext4. Remember to, replace “sdb1” with your partition’s label: 
sudo mkfs.vfat /dev/sdb1

7. Verifying the newly created filesystem
Run the following command in the terminal:
sudo file -sL /dev/sdb1

8. Lastly, execute the following command for ejecting the drive when finished.
sudo eject /dev/sdb

Now, We have successfully formatted the USB storage device and have also created a file system with a partition.
linux-command
Linux-file-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.