Skip to content

Instantly share code, notes, and snippets.

@jprieton
Last active December 10, 2022 08:10
Show Gist options
  • Save jprieton/f36abc75e7b320ecb8abe0f057c6627c to your computer and use it in GitHub Desktop.
Save jprieton/f36abc75e7b320ecb8abe0f057c6627c to your computer and use it in GitHub Desktop.
Quick format a drive as NTFS or ExFAT on Linux
#!/bin/bash
echo "Format a disk as ExFAT"
disk=$1
label=$2
# Example: sudo sh qformat.exfat.sh /dev/sdc "MY_LABEL"
# Check if disk is specified
if [ -z "$disk" ]; then
echo "Usage: $0 device [label]"
exit 1
fi
# Check if disk exists
if [ ! -b "$disk" ]; then
echo "Disk $disk does not exist"
exit 1
fi
# Check if disk is mounted
if grep -qs "$disk" /proc/mounts; then
echo "Disk $disk is mounted"
exit 1
fi
# Check if the label exists
if [ -z "$label" ]; then
label="NO_LABEL"
fi
# Show all disks
# lsblk
# Wipe the disk
sudo wipefs -a ${disk}
# Create a new partition table
sudo parted -s -a optimal ${disk} mklabel gpt
# Create a single partition that takes up the entire disk
sudo parted -s -a optimal ${disk} mkpart primary 0% 100% --script
# Format the partition as exFAT
sudo mkfs.exfat ${disk}1 -n "${label}"
#!/bin/bash
echo "Format a disk as NTFS"
disk=$1
label=$2
# Example: sudo sh qformat.ntfs.sh /dev/sdc "MY_LABEL"
# Check if disk is specified
if [ -z "$disk" ]; then
echo "Usage: $0 <disk> <label>"
exit 1
fi
# Check if disk exists
if [ ! -b "$disk" ]; then
echo "Disk $disk does not exist"
exit 1
fi
# Check if disk is mounted
if grep -qs "$disk" /proc/mounts; then
echo "Disk $disk is mounted"
exit 1
fi
# Check if the label exists
if [ -z "$label" ]; then
label="NO_LABEL"
fi
# Show all disks
# lsblk
# Wipe the disk
sudo wipefs -a ${disk}
# Create a new partition table
sudo parted -s -a optimal ${disk} mklabel gpt
# Create a single partition that takes up the entire disk
sudo parted -s -a optimal ${disk} mkpart primary 0% 100% --script
# Format the partition as NTFS
sudo mkfs.ntfs -Q -L "${label}" ${disk}1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment