Thursday, June 5, 2008

Formatting USB Drive with Linux

This is a guide to format a Thumb drive in Linux. After a bit of Googling I got mkfs, which is used to build a Linux file system on a device. Now for thumb drive we use FAT32 or VFAT filesystem, so we use mkfs.vfat instead.

Syntax (quick format) :
# mkfs.vfat -F32 {device}

For Fedora/RedHat based :
 # /sbin/mkfs.vfat -F32 {device}


I have made a script for easy formatting :

1> Open Terminal :
$ nano format.sh

2> Paste this and save :
#!/bin/bash
# Bash Script for formatting USB drives by GNUger

if [[ $EUID -ne "0" ]]
then
echo "You must be root"
exit 1
fi

device=$(df | tail -n1 | awk '{ print $1; }')
read -p " Is device ($device) correct(y/n) : " opinion1
if [[ $opinion1 == 'n' ]] || [[ $opinion1 == 'no' ]]
then
read -p "Enter device name : " device
fi
echo
echo "Formatting : $device"
echo
read -p "Proceed with formatting(y/n) : " opinion2
if [[ $opinion2 == 'y' ]] || [[ $opinion2 == 'yes' ]]
then
umount $device
read -p "Volume label : (Enter for none) " label
if [[ ${#label} == 0 ]] ; then
/sbin/mkfs.vfat -F32 $device
else
/sbin/mkfs.vfat -F32 -n "$label" $device
fi
exit 1
fi

Attached the file here

3> Then allow the file to execute:
$ chmod +x format.sh

4> Insert your thumb drive and execute :
# ./format.sh


You can also use GParted for formatting and resizing.

No comments: