While most desktop Linux distributions include graphical tools like GParted or GNOME Disks for managing storage, there are many situations where a graphical interface is unavailable. Whether you are managing a headless Ubuntu server over SSH, or simply prefer the speed and precision of the command line, formatting a storage device via text commands is a fundamental administration skill.
By learning how to format a USB drive in the Linux terminal using mkfs, you can quickly erase corrupted partitions and rebuild fresh filesystems (like FAT32 or ext4) using a few simple commands.
Caution: The command line does not ask “Are you sure?” before destroying data. If you format the wrong drive, you will permanently erase your operating system. Please read these instructions carefully.
Step 1: Identify Your USB Drive
Before you can format the drive, you must figure out what name Linux has assigned to it. Storage devices in Linux are typically named /dev/sda, /dev/sdb, /dev/sdc, and so on.
- Plug your USB drive into the computer.
- Open your terminal and type the “list block devices” command:
lsblk - Press Enter.
The terminal will print a tree of all connected drives. Look for a device whose size matches your USB drive (for example, if you plugged in a 16GB flash drive, look for a device listed at around 14.9G).
You will see the main drive (e.g., sdb) and underneath it, the specific partition you need to format (e.g., sdb1). Make a note of the partition name (e.g., /dev/sdb1).
Step 2: Unmount the Drive
Linux will not allow you to format a drive that is currently being used by the system. If the lsblk output shows a “Mountpoint” next to your USB partition (like /media/usb), you must unmount it first.
Run the following command, replacing /dev/sdb1 with your specific partition:
sudo umount /dev/sdb1
If the command returns no output, it was successful. The drive is still physically plugged in, but the operating system has released its grip on the files.
Step 3: Format the Drive Using mkfs
Now you can build a new filesystem on the partition. You have two main choices depending on how you plan to use the USB drive.
Option A: Format as FAT32 (Maximum Compatibility)
If you want to use this USB drive to transfer files between Linux, Windows, and macOS computers, you must format it as FAT32. This is the universal standard for removable flash storage.
To format the partition as FAT32, run:
sudo mkfs.fat -F 32 /dev/sdb1
(Again, double-check that /dev/sdb1 is actually your USB drive before pressing Enter!)
Option B: Format as ext4 (Linux Only)
If this USB drive will only ever be plugged into Linux machines (for example, you are using it to back up a Raspberry Pi or an Ubuntu server), you should format it as ext4. Ext4 is a journaling filesystem that handles large files much better than FAT32 and is significantly more resilient against data corruption.
To format the partition as ext4, run:
sudo mkfs.ext4 /dev/sdb1
Step 4: Verify the Formatting
The formatting process usually only takes a few seconds. To verify that the new filesystem was applied correctly, run the lsblk command again with the filesystem flag:
lsblk -f
Look at your USB partition in the list. Under the FSTYPE column, you should now see either vfat (which means FAT32) or ext4. You can now safely remove the USB drive or mount it to begin copying files.