[root tip] Repartition and format your USB stick

Difficulty: ★★☆☆☆


The scripts partitions and formats any USB device using exFAT filesystem. To avoid accidental format of non removable devices a couple of safeguards exist.

The script will check if the supplied device is valid then it checks if the device is in fact a removable device. If any of these checks fail the script aborts.

The script will ask for user confirmation twice and default to abort on Enter

Use case

Re-purpose the USB stick if you have flashed an ISO to it.

Why? Because writing an ISO using dd writes an ISO9660 filesystem to a small part of the USB - the rest is inaccessible.


Create the folder ~/.local/bin

mkdir -p ~/.local/bin

Create a new file e.g. format-usb.sh

touch ~/.local/bin/format-usb.sh

Make the file executable

chmod +x ~/.local/bin/format-usb.sh

Open the file with your favorite editor e.g. kate

kate ~/.local/bin/format-usb.sh

Copy below content into the file and save it.

TIP: Use the copy to clipboard box in the upper right corner of below text box

#!/usr/bin/env bash
# format removable USB device to exfat with name 'USB STICK'

# don't run as root
if [[ $(whoami) == "root" ]] ; then
    echo "Please run as user. Script will ask for root later."
    exit 1

#!/usr/bin/env bash
# format USB device to exfat

# ensure a device is given
if [[ -z "$1" ]]; then
    echo "No device specified ..."
    echo "Usage: format-usb.sh /dev/sdy"

# get the last part of device path
device="$(echo $1 | cut -d'/' -f3)"
echo "Checking device on => $device"

# create list of available devices
devices="$(lsblk -o NAME | egrep '^sd')"

# check if device is valid - abort if not
if ! [[ $devices =~ (^|[[:space:]])$device($|[[:space:]]) ]]; then
    echo "$1 not found"
    echo "Aborting"
    exit 1

# check if device is removable - abort if not
[[ $(echo $(lsblk -no RM "$1" | head -n 1)) = '0' ]] && \
    echo "Non removable device detected!" && \
    echo "Aborting" && \
    exit 1

# Ask for confirmation
read -r -p "Confirm reformat of '$1' [y/N] " resp
if [[ "$resp" =~ ^([yY][eE][sS]|[yY])$ ]]; then

    # Repeat confirmation question
    read -r -p "Irrevocably format  '$1' [y/N] " resp2
    if [[ "$resp2" =~ ^([yY][eE][sS]|[yY])$ ]]; then

        # will do
        echo "Formatting $1 ..."

        # use gdisk to create new partition table
        # and a single Microsoft basic data partition type
        printf 'o\ny\nn\n\n\n\n0700\nw\ny\n' | sudo gdisk "$1" > /dev/null
        sudo mkexfatfs -n "${LABEL}"  "$1"1 > /dev/null

        # done
        echo "Done"
    echo "Formatting aborted"
    echo "Formatting aborted"


Remove your USB if attached and list devices


Insert your USB device and list the devices


The new device in the list is your USB device e.g. /dev/sdy - format the device using the script

format-usb.sh /dev/sdy


What this script does can probably be done in numerous ways with numerous tools.

Cross posted at root.nix.dk
The list contains validation is taken from this stackoverflow topic



  • Added label to format
  • Cosmetic fix -> send output to /dev/null


  • Fix typo in device check

Thanks for this work.

Is this not achievable via parted?


I don’t know - I am using gpart gdisk because I can use printf to pipe the commands.

not in the script provided… :wink:

That’s what I meant to say :man_facepalming:

Thx ! Is there a way to have the created partition named differently by the script (NOT Microsoft basic data) ?

Microsoft Basic data is not a label - but a partition type.

Just change the formatting command in the script to include a label - like

sudo mkexfatfs -n USBSTICK "$1"1

great, thx !

I added your suggestion to the original script.

If you want another label - change the LABEL variable at the top of the script.

cool ! Already using new your script, very handy !