how to format a disk drive in linux

Formatting a data storage device such as a hard disk or USB flash drive, involves wiping out or deleting the entire stored data on the drive and installing a brand new address table on the drive. This process will also test all the sectors on the drive for reliability and mark the bad ones as unusable. Hence, when you format a drive it is a process to start anew with your existing hard drive.

There are several reasons and situations why you might want to format a drive …

  • New Drive: If you are installing a brand new drive which has not been formatted before, you will have to format to make the drive compliant with the operating system that you are installing or to use a specific file system of your choice.
  • Old or Bad Drive: If your drive has some bad sectors then reformatting will identify these bad sectors. This could be a good way to re-use an older drive much more reliably.
  • Corrupted or Infected Drive: If your drive has been infected with a virus or malware then formatting is a good idea to ensure that nothing is left behind from the infected drive.
  • Selling: If you are selling either the hard drive by itself or your machine then formatting your drive is a very good idea to guard against data theft.
  • Just because: maybe you just want to reformat the drive and start over or test out how reformatting works….

As mentioned above, your drive should be partitioned before it can be formatted. You can use the fdisk command to partition the drive or delete and create new ones. The drive should have alteast one partition, but usually it may have multiple partitions depending on your requirements. You may also want to consider re-partitioning the disk before reformatting. If you intend to completely clean out the disk, consider deleting all the existing partitions on the disk and partitioning them again before formatting.

If this is your primary drive, and you are installing Linux then you might want at least three partitions: one for the boot, one for swap space and one for your files …etc.

You need to partition your disk before you can format it. In this post we are going to assume that you have at least one partition, namely /dev/sda1. If you don’t know what the device name or partitions you have on your system, start with listing the hard drives on your system. After that you can list your partitions for the disk that you want to format.

Before you format a drive, you need to decide on the file system type that you want to use. There are several different types of file systems available on Linux, each with its own advantages and disadvantages. The most common and popular file system types in Linux are ext3, ext4, reiserfs and xfs. You will also need the name of the partition that you want to format.

mkfs (Make File System) is a wrapper command that is often found in most Linux distributions. It supports creation of several file system types like ext2, ext3, ext4, bfs, vfat and other file system types that are supported by your kernel and the OS. To find all the types that are supported, check the mkfs files in your /sbin folder.

bash$ ls -a /sbin/mkfs.*

Sample output:

/sbin/mkfs.bfs /sbin/mkfs.cramfs /sbin/mkfs.ext2 /sbin/mkfs.ext3 /sbin/mkfs.ext4 /sbin/mkfs.ext4dev /sbin/mkfs.minix /sbin/mkfs.reiserfs /sbin/mkfs.msdos /sbin/mkfs.vfat

All these files with extensions are the file systems that are supported on your system. The extension to the mkfs command is the file system type that is supported on your system.

Disk Partition - Supported FS types

The generic command to  perform a format of a partition is

bash$ mkfs -t <file system type> <partition name>

You can also use the command specific to the file system type (as mentioned in the example below) instead of specifying the -t option to the mkfs command.

The following command performs a format of the sda1 partition using the ext3 file system type

bash$ mkfs -t ext3 /dev/sda1


bash$ mkfs.ext3 /dev/sda1

The commands for the other file system types follows the same general pattern. Substitute the -t option to the file system type you want. For ext4 filesystem, you will replace the ext3 in the above command with ext4.