How to format a disk using mkfs command in Linux


The data on a computer can be stored on different storage devices such as hard disk, USB drive, etc. This data need to be positioned in a systematic way so that whenever needed operating system can fetch it easily. So filesystem defines how data will be stored and retrieved on a storage device.

A storage device must be formatted with a file system before it can be mounted and accessed on a Linux system. The mkfs command is used for making filesystem on a disk which is also known as disk formatting.

This article will guide you in formatting a disk using mkfs command in Linux operating system.

Syntax of mkfs command

The basic syntax of the mkfs command is –

mkfs.fs_type device

Or

mkfs -t fs_type device

Where,

fs_type – is the filesystem type i.e. ext2, ext4, NTFS, etc.

device – is the name of the partition or disk for example /dev/sda.

List the supported filesystems

Enter the following command and then hit the Tab button twice to see the list of the filesystem in which a disk can be formatted using mkfs command –

# mkfs

Now you can see the different filesystems here which include ext4, NTFS, fat, etc. The mkfs command is separated from the filesystem using a dot in between them.

Formatting a disk example

You need to have root user privileges for formatting a disk. Here we will format a /dev/sdb that has a size of 32 GB.

To format /dev/sdb in ext4 format use –

# mkfs.ext4 /dev/sdb

If you want to format the disk in FAT32 filesystem then use-

# mkfs.fat /dev/sdb

To format the disk in NTFS format use –

# mkfs.ntfs /dev/sdb

Create a mount point for the filesystem

Use the following command to create a mount point directory for the formatted disk-

# mkdir /mdisks

Mount the new filesystem

To mount the new filesystem to created mount point which is /mdisks by using the given command –

# mount /dev/sdb /mdisks

Make changes permanent

If you want /dev/sdb to mount at the mount point /mdisks automatically when the system boots then you need to add the given line into /etc/fstab file.

First, open /etc/fstab file using a text editor and then add –

/dev/sdb   /mdisks  ext4 default 0 2

Where,

/dev/sdb – Filesystem,  partition, or disk name

/mdisks – Mount point

ext4 – The filesystem in which disk is formatted

defaults – mount options

0– Indicates that filesystem doesn’t require dump

2– Means it is not a root file system, the root system is specified with a value 1

Now save the file and exit from the editor.

Conclusion

After reading this now you can format a disk or partition to a specific filesystem on Linux. Now if you want to say something on this topic then write us in the comments below.

Leave a Comment

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