$ usb-formatter

A small Bash script that wipes a USB stick and reformats it as NTFS (default) or exFAT, so Windows can read it after Ubuntu mkusb chewed it up. It refuses to touch anything that looks like a system disk.

what it does

One pass, five steps. wipefs clears the old signatures, sgdisk writes a fresh GPT with a single full-size partition, then mkfs.ntfs or mkfs.exfat lays down the filesystem. Before any of that, every partition on the device is unmounted through udisksctl so udisks2 does not auto-remount mid-format.

If the wipe fails because something is still holding the device, the script runs its own diagnostic (lsblk, mount, fuser, pvs, lvs) and prints what is in the way before bailing out.

install & run

Pull the script, mark it executable, preview the run, then commit:

curl -O https://raw.githubusercontent.com/cocodedk/usb-formatter/main/format-usb.sh
chmod +x format-usb.sh
./format-usb.sh /dev/sdX KINGSTON --dry-run
sudo ./format-usb.sh /dev/sdX KINGSTON
Tip
Always run with --dry-run first. It does not need root and prints every command it would execute, so you can confirm the right device before handing it sudo.

flags

flageffect
--ntfsFormat as NTFS. Default. Reads on Windows out of the box.
--exfatFormat as exFAT instead. Better for files over 4 GB on cameras and TVs.
-n, --dry-runPrint every step without changing anything. No root needed.
-d, --diagnoseInspect the device only — mounts, holders, LVM. Use when a previous run failed busy.
-h, --helpPrint usage and exit.

safety guards

removable-only
Reads /sys/block/<dev>/removable. If it is not 1, the script refuses. Internal SSDs and NVMe drives never pass this check.
system mountpoint refusal
If any partition on the device is mounted at /, /home, or /boot, the run aborts before anything is touched.
active swap refusal
If a partition is currently in use as swap (swapon --show), the script will not wipe it.
cooperative unmount
Unmount goes through udisksctl unmount --no-user-interaction, then falls back to umount and finally lazy umount -l. udisks2 is told not to remount.
auto-diagnose on failure
If wipefs fails, the diagnostic block runs automatically and prints holders, mounts, and LVM state before the script exits.
YES confirmation
In live mode you must type the literal string YES to proceed. --dry-run skips this and changes nothing.
Warning
This script destroys data. It cannot recover anything from the target device. Read the dry-run output, then read it again, then run it.

when something goes wrong

The most common failure is "device busy" — a file manager opened a partition the moment you plugged the stick in. Run the diagnostic to see the holder:

sudo ./format-usb.sh /dev/sdX --diagnose

You get an lsblk tree, all live mount entries, the output of fuser -v on every partition node, and the LVM picture from pvs and lvs. Close the file manager (or kill the holder), unplug, replug, retry.