1. Overview

In this tutorial, we’ll discuss how to quickly format a USB drive. First, we’ll learn how to create a partition table on a USB disk. Next, we’ll look at the formatting process for each file system.

Finally, we’ll completely wipe out the data from the USB drive with tools like dd and shred.

2. Creating a Partition Table

A partition table describes how the storage space of a block device is divided into separate sections. We refer to these sections as partitions. The partitions table provides the essential metadata about the start and end of these partitions.

There are two most commonly used partitions tables:

  • Master Boot Record (MBR) uses a 32-bit partition table scheme and supports up to four primary partitions
  • GUID Partition Table (GPT) uses 64-bit partitions table scheme and supports up to 128 primary partitions

In this article, we’ll focus on GPT for several reasons:

  • It supports larger storage capacities
  • It allows for a large number of partitions
  • It supports both primary and logical partitions
  • It’s a natural fit for UEFI, which is used in most modern systems

2.1. Checking the Attached USB Device

On Linux, we have access to the fdisk utility, which comes preinstalled with the util-linux package. We can use fdisk for disk and partition management.

However, we should first figure out the block device that’s mapped to our actual physical USB drive. For that purpose, we’ll use the lsblk utility:

$ lsblk
NAME   MAJ:MIN   RM   SIZE    RO   TYPE   MOUNTPOINT
sda    8:16      0    512G    0    disk
├─sda1 8:17      0    512G    0    part   /
sdb    8:0       0    16G     0     disk
└─sdb1 8:1       0    15.9G   0    part   /mnt

Here, we are interested in the /dev/sdb block device, which is mapped to a 16 GB USB drive. First, we’ll unmount the /dev/sdb1 partition:

$ umount /dev/sdb1

2.2. Creating GPT on the USB Drive

Now, let’s open this up in fdisk:

$ sudo fdisk /dev/sdb

Next, we’ll enter “g” to create GPT:

Command (m for help): g
Created a new GPT disklabel (GUID: E98D9321-1A25-4D71-9742-3C7698EABCDB).

We can now create a new partition on it by typing in “n” and entering the default values:

Command (m for help): n
Partition number (1-128, default 1): 
First sector (2048-33554431, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-33554431, default 33554431): 

Created a new partition 1 of type 'Linux filesystem' and of size 16 GiB.

Now, we’ll write the changes to our USB drive by entering “w“:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

That’s it! We’re now ready to format this partition.

3. Formatting a USB Drive

In Linux, we can use a variety of different file systems. Each file system has its own unique set of utilities. For example, we can use the mkfs.ext4 utility to create a new file system on a partition.

Similarly, there are other utilities tailored toward specific file systems for different purposes. However, we’re interested in formatting and creating file systems, which we’ll discuss next.

3.1. ext4

On Linux, we can create and format a partition as ext4 (Extended File System) using the mkfs.ext4 utility. By default, it’s installed on most Linux distributions as part of the e2fsprogs package.

Let’s create and format the file system on /dev/sdb1:

$ sudo mkfs.ext4 -L Pendrive -m 1 -b 4096 /dev/sdb1

Let’s break this down:

  • -L specifies the label for the partition
  • -m sets the reserved blocks percentage, which is 1 (1%) in this case
  • -b specifies the block size, which we’ve set to 4096 bytes

Once we run the command, it wipes out the existing data and creates a new ext4 partition on the disk.

3.2. NTFS

Often, we need to deal with USB drives that use the NTFS (New Technology File System). Fortunately, there is the ntfs-3g utility that provides the mkfs.ntfs tool:

$ sudo mkfs.ntfs --fast --label Pendrive /dev/sdb1

In the command, –fast performs a quick format.

3.3. FAT, FAT16, VFAT and FAT32

FAT (File Allocation Table), FAT16, VFAT (Virtual FAT), and FAT32 are related file systems. FAT16 is an upgrade over the original FAT that provides larger partitions and file sizes. In addition, VFAT is an extension to the original FAT that allows for longer filenames.

On the other hand, FAT32 overcomes the limitations of both FAT and FAT16. It supports larger partition sizes, file sizes, and long filenames. Indeed, it’s more widely used as compared to the other variants.

Essentially, the method for formatting these file systems is the same. Therefore, we can use both mkfs.fat and mkfs.vfat, which come with the dosfstools package:

$ sudo mkfs.fat -F 32 -n Pendrive /dev/sdb1

Similarly, we can also use mkfs.vfat:

$ sudo mkfs.vfat -F 32 -n "Pendrive" /dev/sdb1

Additionally, we can also specify 12 and 16 for the -F option.

3.4. exFAT

exFAT is a separate file system that provides additional improvements over the FAT32 file system. In particular, these improvements include larger partition sizes, file sizes, and optimization for USB drives and SD cards.

We can create and format an exFAT partition by using the mkfs.exfat utility, which comes with the exfatprogs package:

$ sudo mkfs.exfat -n Pendrive /dev/sdb1

3.5. Btrfs

For Btrfs file systems, we should first install the btrfs-progs package. Then, we can use the mkfs.btrfs utility to format the partition as Btrfs:

$ sudo mkfs.btrfs -L Pendrive /dev/sdb1

3.6. XFS

Similarly, we can install the xfsprogs package to use the mkfs.xfs utility for XFS file systems:

$ sudo mkfs.xfs -L Pendrive /dev/sdb1

4. Wiping Data From a USB Drive

When we format a drive, it doesn’t necessarily erase all the data. Indeed, it does change the file system structures. However, some data could remain recoverable.

For that purpose, we can overwrite the existing data with zeroes or random garbage to ensure that the data remains unrecoverable. For this purpose, we can use two specific tools: dd and shred.

4.1. dd

dd (data duplicator/disk dump) is a utility that can copy data between locations as well as wipe out data.

By default, it’s installed on most Linux distributions. So, we can readily use it to wipe out the data on a USB partition:

$ sudo dd if=/dev/zero of=/dev/sdb1 bs=1M

It’s important to double-check the output file. Once we run the command, it’s extremely difficult to recover the lost data.

4.2. shred

shred overwrites files and devices repeatedly with random data. Thereby making the data harder to recover.

Like dd, it’s also installed on most Linux distributions:

$ sudo shred -n 3 -z /dev/sdb1

Let’s break this down:

  • -n specifies the number of repetitions
  • -z zeros the partition to hide the shredding process

5. Conclusion

In this article, we learned how to create a partition table on a USB disk. Then, we used a wide range of tools for different file systems to format the USB drive partition.

Finally, we saw how to wipe out the data from a USB drive to make it difficult to recover.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.