How To Format Usb On Linux

How To Format Usb On Linux

Reading time1 min
#Linux#USB#CommandLine#LinuxUSB#USBFormatting#CLI

Mastering USB Formatting on Linux: A Command-Line Approach Beyond GUI Tools

Forget relying on bulky GUI tools; the real power lies in mastering lightweight, scriptable Linux commands that give you precise control over USB formatting—perfect for automation and troubleshooting.

Whether you’re prepping a USB stick for a fresh install, clearing out old data, or creating a bootable drive, knowing how to format your USB drive directly from the command line is an invaluable skill. GUI tools like GParted or Disks are handy but can be slow, limited, or unavailable in headless servers and minimal Linux setups.

This guide will walk you through formatting USB drives on Linux using native command-line utilities—including lsblk, fdisk, mkfs, and more—with clear examples so you can confidently manage your storage devices without leaving the terminal.


Why Format Your USB Drive Using the Command Line?

  • Speed & Precision: CLI formatting is fast and doesn't require loading bulky interfaces.
  • Automation Friendly: Easily script formatting tasks for batch jobs or repeatable setups.
  • Works Everywhere: Available by default on almost all Linux installations, including servers.
  • Better Troubleshooting: CLI tools provide detailed output to help debug errors.

Step 1: Identify Your USB Drive

Before formatting anything, it’s critical to confirm the exact device identifier (e.g., /dev/sdb) to avoid erasing the wrong disk.

lsblk

Output example:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
├─sda1   8:1    0   450M  0 part /boot
└─sda2   8:2    0 465.3G  0 part /
sdb      8:16   1  14.5G  0 disk
└─sdb1   8:17   1  14.5G  0 part /media/usb

In this example, /dev/sdb is the USB drive. Double-check the RM (removable) column helps identify external devices.

Warning: Make sure you select the correct device! Formatting destroys all data on it.


Step 2: Unmount Any Mounted USB Partitions

If your device is auto-mounted (e.g., /media/usb), unmount all partitions before formatting:

sudo umount /dev/sdb1

You may need to unmount multiple partitions if present.


Step 3: Create a New Partition Table and Partition

Using fdisk

Start by launching fdisk on your USB device:

sudo fdisk /dev/sdb

Once inside:

  • Type o and hit Enter — creates a new empty DOS partition table.
  • Type n and hit Enter — to create a new partition:
    • Select primary partition by typing p
    • Choose partition number (1 by default)
    • Press Enter twice to accept default start/end sectors (uses full space).
  • Type t and hit Enter — change partition type:
    • For a typical FAT32 format, enter c (W95 FAT32 (LBA))
  • Type w and hit Enter — write changes to disk and exit.

Alternative Partitioning Tools

If you prefer scripts or fewer steps, tools like parted allow quick scripting:

sudo parted /dev/sdb --script mklabel msdos mkpart primary fat32 1MiB 100%

Step 4: Format the Partition with Filesystem of Choice

Now format /dev/sdb1 to an appropriate filesystem depending on your use case:

  • FAT32 — Most universally supported (Windows, macOS, Linux):
sudo mkfs.vfat -F32 /dev/sdb1
  • exFAT — For large files (>4GB), modern OS support required:
sudo mkfs.exfat /dev/sdb1

(Note: You might have to install exfat-utils or equivalent first.)

  • ext4 — Linux-native filesystem not widely supported by Windows/macOS:
sudo mkfs.ext4 /dev/sdb1

Step 5: Verify Everything Worked

Run lsblk -f to confirm filesystem types:

lsblk -f /dev/sdb

Example output snippet:

NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sdb                                                         
└─sdb1 vfat         ABCD-1234                            

Optionally, mount it manually and check contents:

mkdir ~/usbtest && sudo mount /dev/sdb1 ~/usbtest
ls ~/usbtest
sudo umount ~/usbtest && rmdir ~/usbtest

Bonus Tips for Power Users

Formatting Multiple Drives Quickly With a Script

Save this snippet as format_usb.sh, replace /dev/sdX with your device in each run:

#!/bin/bash

USB_DEV="/dev/sdX"

echo "Unmounting partitions..."
sudo umount ${USB_DEV}?*

echo "Creating partition table..."
sudo parted ${USB_DEV} --script mklabel msdos mkpart primary fat32 1MiB 100%

echo "Formatting partition..."
sleep 2 # wait for kernel to recognize partition

sudo mkfs.vfat -F32 ${USB_DEV}1

echo "Done!"

Run with:

chmod +x format_usb.sh && ./format_usb.sh

Creating Bootable USB Drives

After formatting, use tools like dd for raw image copies if needed:

sudo dd if=path/to/iso of=/dev/sdb bs=4M status=progress oflag=sync

This is outside our pure "formatting" scope but commonly paired in workflows.


Conclusion

Mastering USB formatting via command line on Linux gives you ultimate flexibility and reliability. Once you’re comfortable with identifying devices (lsblk), partitioning (fdisk, parted), and making filesystems (mkfs.*), you can manage any drive quickly—even remotely on systems without graphical environments.

Next time you plug in a mysterious drive or want a clean slate fast—skip the GUI bloat and harness these powerful commands instead!


If you found this guide helpful or want me to cover scripting advanced automation with CLI formatting next—let me know in the comments below!