This article will show you how to format a USB stick or external USB hard drive from the Linux command line/shell for FAT, NTFS, and EXT file systems.
Plug In the Drive
The first step – plug in your USB stick or external hard drive and give it a few moments to be detected.
Find the Drive
Next, find the drive you just plugged in using the fdisk command to list (-l) the attached storage devices:
sudo fdisk -l
We’ll be using the sudo command frequently – many of these tasks require administrative privileges.
The fdisk command will output a list of storage devices attached to your system. Among them (hopefully last in the list to make it easy to find) will be the device you just plugged in:
Disk /dev/sdb: 29.26 GiB, 31406948352 bytes, 61341696 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: 0xfdb38d34 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 61341695 61339648 29.3G c W95 FAT32 (LBA)
Above, you can see the device /dev/sdb (the physical USB drive) and /dev/sdb1 – a FAT partition on that drive.
The device names sdb/sdb1 may differ on your system!
Make Sure the Drive Isn’t Mounted
You can’t format a drive that is in use, so the drive must be unmounted from the file system. Some systems will have mounted the drive automatically, and some will not have.
sudo umount /dev/sdb1
The above command will unmount /dev/sdb1.
Format the Drive
The mkfs set of commands will create an empty file system on a given device. Not all filesystems are supported on all operating systems, so choose one that works for you.
Be warned – all files will be removed – so make sure you’re sure you want to remove everything on the drive and that you use the correct path to the device to avoid formatting the wrong drive.
Format for EXT4 Filesystem
The following command will format using the EXT4 filesystem for use on Linux Systems:
sudo mkfs.ext4 /dev/sdb1
Format for FAT/vFAT Filesystem
The following command will format using the FAT filesystem for use in Linux, Windows, and macOS:
sudo mkfs.vfat /dev/sdb1
Format for NTFS Filesystem
The following command will format using the NTFS Filesystem for use on Windows and some supported Linux distributions:
sudo mkfs.ntfs /dev/sdb1