USB partition script

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.

touch ~/.local/bin/

Make the file executable

chmod +x ~/.local/bin/

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

kate ~/.local/bin/

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: /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 /dev/sdy


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

The list contains validation is taken from this stackoverflow topic


2021-01-11 06:44:00 Europe/Copenhagen

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

2020-09-02 17:54:00 Europe/Copenhagen

  • Fix typo in device check