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 …

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 (*M*a*k*e *F*ile *S*ystem) 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.

mkfs-partition.png

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

Or

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.