How to format a disk using mkfs command in Linux
 

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.

Previous Join the Hacktoberfest and Become an open-source Ninja
Next [ubuntu] WoW
 

About author

You might also like

Linux Topics 0 Comments

Top Command Usage With Example In Linux/Unix

This post was last updated on August 1st, 2020 at 08:47 pmThe top is one of the most frequently used command in Unix like operating systems for the administrative tasks.

How To 0 Comments

How To Find UID And GID Using Id Command In Linux/Unix?

This post was last updated on August 1st, 2020 at 12:45 pmLinux/Unix are multi-user operating systems that means different users can access and use system resources simultaneously. To access the

Commands 0 Comments

Using tree command in Linux

The tree is a command which is available for both Linux and Windows. It recursively displays the content of directories on your system. If this command is used without an

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply