How to Format USB Flash drive from command line on Linux / Ubuntu

By | May 2, 2023

If there is a lot of content on the usb flash drive and you want to erase all of it quickly then a simple way to do it is by formatting it. There are many gui tools available on linux to view and format partitions on storage devices like ssds and flash drives.

These include Gnome Disks, Gparted, KDE Partition manager and so on. Besides gui tools drives can also be formatted from the command line in linux, and the process is quite simple.

Format from terminal

Here are the simple steps

1. Plug-in the drive: First insert the usb drive. However do not mount it from any file manager like Dolphin or Nautilus. In order to format the drive partitions it must stay unmounted.

2. Check the partition device path: Note down the partition path, which looks something like /dev/sdaX or /dev/sdbX. Once the system has detected the drive we can use the blkid, lsblk or mount command to check the partition devices. Here is a quick example

sudo lsblk -e7 -o "NAME,PTTYPE,FSTYPE,SIZE,LABEL,PARTLABEL,PATH,PHY-SEC,VENDOR"
$ sudo lsblk -e7 -o "NAME,PTTYPE,FSTYPE,SIZE,LABEL,PARTLABEL,PATH,PHY-SEC,VENDOR"
NAME   PTTYPE FSTYPE    SIZE LABEL                  PARTLABEL PATH      PHY-SEC VENDOR
sda    dos            111.8G                                  /dev/sda      512 ATA     
└─sda1 dos    ext4     95.4G                                  /dev/sda1     512 
sdb    dos            111.8G                                  /dev/sdb      512 ATA     
└─sdb1 dos    swap     95.8G                                  /dev/sdb1     512 
sdc    gpt            447.1G                                  /dev/sdc      512 ATA     
└─sdc1 gpt    ext4      400G                                  /dev/sdc1     512 
sdd    dos    iso9660  14.5G Fedora-KDE-Live-36-1-5           /dev/sdd      512 SanDisk 
├─sdd1 dos             12.4G                                  /dev/sdd1     512 
└─sdd2 dos    vfat      9.9M ANACONDA                         /dev/sdd2     512 
$

Note the SanDisk usb drive in the above output which is mapped to device sdd with 2 partitions are /dev/sdd1 and /dev/sdd2 respectively.

3. Format with mkfs: Next format the partition using the mkfs command and specify the desired file-system. To format with FAT file-system use the following command:

sudo umount /dev/sdd1
sudo mkfs -t vfat /dev/sdd1
sudo eject /dev/sdd1

First unmount it, then create filesystem/format using the mkfs command and then eject when done.

Here is what the output of the mkfs command would look like:

$ sudo mkfs -t vfat /dev/sdd1
[sudo] password for enlightened: 
mkfs.fat 4.2 (2021-01-31)
$

Now your partition is ready for use. Start by copy some files to it. Can also use gparted so that you dont have to remember the commands.

Note that the following commands are equivalent:

sudo mkfs -t vfat /dev/sdd1
or
sudo mkfs.vfat /dev/sdd1

For other file-systems use any of the following commands:

  • mkfs.bfs
  • mkfs.cramfs
  • mkfs.exfat
  • mkfs.ext2
  • mkfs.ext3
  • mkfs.ext4 - Format to Ext4 file-system
  • mkfs.fat
  • mkfs.minix
  • mkfs.msdos
  • mkfs.ntfs - Format to NTFS file system
  • mkfs.vfat - Format to FAT-32

Other commands to check partition device path

Once you know the device name, you can use fdisk command as well to list the partitions in that particular drive:

$ sudo fdisk -l /dev/sdd
Disk /dev/sdd: 14.53 GiB, 15597568000 bytes, 30464000 sectors
Disk model: Cruzer Blade    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x4953725b

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sdd1       4388864 30463999 26075136 12.4G 83 Linux
/dev/sdd2           172    20455    20284  9.9M ef EFI (FAT-12/16/32)

Partition table entries are not in disk order.
$

The mount command can also be used to get the partition device path, however it will show only if the partition is mounted. So first you have to mount it, view using mount command then un-mount before formatting, which is tedious.

$ mount | grep -i '/dev/s'
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64)
/dev/sda1 on /var/snap/firefox/common/host-hunspell type ext4 (ro,noexec,noatime,errors=remount-ro)
/dev/sdc1 on /media/enlightened/a935afc9-17fd-4de1-8012-137e82662ff0 type ext4 (rw,nosuid,nodev,relatime,errors=remount-ro,uhelper=udisks2)
/dev/sdd2 on /media/enlightened/ANACONDA type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)
$

Find out the device from the mount output. In this case it is /dev/sdb1

Formatting unallocated space

The problem with the above approach is that it cannot detect un-allocated free space that is not inside any partition. To view the unallocated space on any drive use the parted command as follows:

$ sudo parted /dev/sdd print free
Model: SanDisk Cruzer Blade (scsi)
Disk /dev/sdd: 15.6GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
        1024B   88.1kB  87.0kB           Free Space
 2      88.1kB  10.5MB  10.4MB  primary  fat16        esp
        10.5MB  2247MB  2237MB           Free Space
 1      2247MB  15.6GB  13.4GB  primary

$

In the above command we need to specify the drive device name followed by options "print free". Note the table contains entries marked "Free Space" The 3rd entry in this case indicates about 2.2 GB of free un-allocated space lying on the usb drive.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

One Comment

How to Format USB Flash drive from command line on Linux / Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *