How to Format a Drive in Linux?

Several storage devices are present as the file systems in Linux that are used for storing the data. One hard drive is normally present in the system and can have several partitions. Sometimes, we need to format the drive for different reasons, such as deleting the data, removing the errors, or changing the file system.

The formatting of a drive prevents the system from storing corrupt data and deletes unnecessary files, and can be done in several ways that are as follows:

Let’s discuss the file system first.

Types of File Systems in Linux

There are several file systems used for disk partitions; the common file systems are ext4, vfat, NTFS, and xfs.

To check the block devices available in the system, the “lsblk” (list block devices) command is used:

$ lsblk

Similarly, to check the file system of the block devices or drives in the system, the “f” option is used as done below:

$ lsblk -f

The output shows that the /dev/sda2 has a “vfat” file type while /dev/sda3 partition is of “ext4” file type.

Method 1: Format a Drive Using fdisk Command

The fdisk command is utilized to create, delete, or modify the partitions on a drive. To delete a partition of a disk, the below steps can be followed:

Start the “fdisk” command by running the below command:

$ sudo fdisk /dev/sda

To delete an existing partition disk, enter the “d” command to format and select the number of disks you want to format as shown below:

The disk will be formatted.

Then use the “w” key to save the changes to the drive and exit the fdisk command:

The fdisk command is very useful while working with the drives, such as the “n” option is used to create a new disk, and the “p” option is used to create the primary partition.

Method 2: Format a Drive Using gdisk Command

The gdisk command is used for GUID partition Table disks. To format a disk, use the gdisk command by executing the below command:

$ sudo gdisk /dev/sda

Now, press the “d” shortcut key to delete the drive in Linux.

Use the “w” key to save the changes to the drive and exit the fdisk command:

The disk number 1 is formatted, and changes are saved in the system.

Method 3: Format a Drive Using GNOME Disks (GUI)

To format a drive using the GNOME Disks, the below step will be followed:

Press the Windows + A shortcut keys to open the search bar and search the “disks” to open it:

The below interface will show up having the details of the disks and partitions as shown below:

Select the disk you want to format and click the hamburger icon as indicated below. Then choose the “Format Disk” option to format the disk:

It will ask you to enter the password to confirm the format disk; after entering the password, the selected disk will be formatted.

Bonus Tip: Format a Specific Partition or Entire Drive

The mkfs command can be used to create or format a partition or a disk; we must delete all the existing partitions to format the entire drive. To format a partition with the mkfs, we need to write the extension and the drive name with the mkfs command.

The below command formats a drive with the file system “ext4”:

$ sudo mkfs.ext4 /dev/sda1

It will format the entire “/dev/sda1” disk having the ext4 file system.

To format a partition having the “vfat” file type, the below command is used to format it:

$ sudo mkfs.vfat /dev/sda1
$ sudo mkfs -t vfat /dev/sdb1

Similarly, the below commands will be utilized to format the disk of the “ntfs” file system:

$ sudo mkfs.ntfs /dev/sda1
$ sudo mkfs -t ntfs /dev/sdb1

Moreover, if you have an “xfs” file system, you can format that drive by executing the below command in the terminal:

$ sudo mkfs.xfs <target_partition>
$ sudo mkfs.xfs -f <target_partition>

Once all the partitions from the drive are removed, the entire disk will be formatted.

Conclusion

To format a drive in Linux, we can use the fdisk, gdisk, mkfs command-line methods, while the GNOME Disks GUI method can also be used. We can format a specific partition of the drive or all the partitions to format the entire drive. Moreover, we can format the entire storage drive directly using the methods explained in this write-up.