As a person working with operating systems, machines and electronic devices, you’ll often need to format Micro SD card, a USB drive/Flash drive with fat32 partition or something similar. This could be to write OS data to it, copy files from one system to another as well as use it as portable storage medium. In this guide we will look at how you can format a USB drive and SD card on any Linux system using parted command line.
In a Linux operating system, there are graphical tools such as GParted and GNOME Disks which can be used in a GUI to format SD Cards, USB Drives and Flash Drive. This guide is focused on using command line tool (parted). Other tools such as fdisk, gdisk, cfdisk, sfdisk can also be used.
Before you begin
You need to have GNU Parted application installed in your Linux system before you begin. GNU Parted is a program for creating and manipulating partition tables. Its frontend is GParted.
Install GNU Parted in your Linux machine using the commands shared below.
--- Debian / Ubuntu ---
$ sudo apt update
$ sudo apt -y install parted
--- Fedora / CentOS ---
$ sudo yum -y install parted
$ sudo dnf -y install parted
--- Arch Linux / Manjaro ---
$ sudo pacman -S parted
You should also connect your SD Cards or USB/Flash Drive to your computer and confirm the OS can see it.
The lsblk
is a Linux command line tool used to list information about all available or the specified block devices.
$ lsblk --all NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:0 1 14.9G 0 disk nvme0n1 259:0 0 238.5G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 237.5G 0 part ├─fedora-root 253:0 0 70G 0 lvm /sysroot ├─fedora-swap 253:1 0 7.7G 0 lvm [SWAP] └─fedora-home 253:2 0 159.8G 0 lvm /var/home
My Flash drive in /dev/sdb. When a USB device is connected to your computer, dmesg – A tool to print or control the kernel ring buffer, will show connection info.
$ dmesg .... [ 6209.409187] usb-storage 1-1:1.0: USB Mass Storage device detected [ 6209.409321] scsi host0: usb-storage 1-1:1.0 [ 6209.409406] usbcore: registered new interface driver usb-storage [ 6209.412727] usbcore: registered new interface driver uas [ 6210.741626] scsi 0:0:0:0: Direct-Access SMI USB DISK 1100 PQ: 0 ANSI: 4 [ 6210.742549] sd 0:0:0:0: Attached scsi generic sg0 type 0 [ 6210.743829] sd 0:0:0:0: [sdb] 31129600 512-byte logical blocks: (15.9 GB/14.8 GiB) [ 6210.745121] sd 0:0:0:0: [sdb] Write Protect is off [ 6210.745128] sd 0:0:0:0: [sdb] Mode Sense: 43 00 00 00 [ 6210.746338] sd 0:0:0:0: [sdb] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA [ 6210.868622] usb 1-1: reset high-speed USB device number 8 using xhci_hcd [ 6211.326903] sdb: [ 6211.332089] sd 0:0:0:0: [sdb] Attached SCSI removable disk
Format SD Card, USB Drive, Flash Drive on Linux with parted
Confirm parted is installed.
$ parted --version
parted (GNU parted) 3.2.153
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3 : GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by .
Confirm the name of an SD card, Flash drive to be formatted.
$ lsblk --all
Before we format our usb device, we’ll first erase filesystem or partition-table signatures which may exist on the device.
$ sudo wipefs --all --force /dev/sdb /dev/sdb: 5 bytes were erased at offset 0x00008001 (iso9660): 43 44 30 30 31 /dev/sdb: 2 bytes were erased at offset 0x000001fe (dos): 55 aa /dev/sdb: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
Using Parted to Format SD Card, USB Drive, Flash Drive on Linux
Parted has two modes: command line and interactive. Parted should always be started with:
$ sudo parted device
where:
- device is the SD Card, USB Drive or Flash Drive to edit. If you omit the device argument, parted will attempt to guess which device you want.
We’ll use Interactive mode which simplifies the partitioning process and reduces unnecessary repetition by automatically applying all partitioning commands to the specified device.
In my scenario, the command I’ll execute to start operating on a device is:
$ sudo parted /dev/sdb GNU Parted 3.2.153 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
You will notice that the command-line prompt changes to (parted)
To see a list of the available commands, enter:
(parted) help
Step 1: Create new partition table
You need to create or recreate the partition table of your device – For first partitioning, or to change the type of its partition table.
To create a new Master Boot Record/MS-DOS partition table:
(parted) mklabel msdos
Confirm:
(parted) p Model: SMI USB DISK (scsi) Disk /dev/sdb: 15.9GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags (parted)
For GPT partition table, you’ll use;
(parted) mklabel gpt
Step 2: Create a partition on device
You can decide the number and size of the partitions the devices should be split into. The command syntax used to create partitions is:
(parted) mkpart part-type fs-type start end
Where:
- part-type is one of
primary
,extended
orlogical
– Only meaningful for MBR partition tables. - fs-type is an identifier for setting a 1-byte code that is used by boot loaders to “preview” what kind of data is found in the partition, and act accordingly if necessary.
- start is the beginning of the partition from the start of the device.
- end is the end of the partition from the start of the device (not from the start value).end
I’ll create a partition starting at 1MiB and ending at 100%.
(parted) mkpart primary fat32 1MiB 100%
Similar use cases include (Don’t run this – just examples)
# Create an ext4 partition
(parted) mkpart primary ext4 1MiB 100%
# Create an XFS partition
(parted) mkpart primary xfs 1MiB 100%
# Create two ext4 partitions
(parted) mkpart primary ext4 1MiB 5GiB
(parted) mkpart primary ext4 5GiB 100%
Confirm the changes we’ve made so far:
(parted) p Model: SMI USB DISK (scsi) Disk /dev/sdb: 15.9GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 15.9GB 15.9GB primary fat32 lba
Step 3: Format the partition
The last step is to format the partition to the filesystem type of your choice.
I’ll format by USB device partition to FAT32.
(parted) quit Information: You may need to update /etc/fstab. $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:0 1 14.9G 0 disk └─sdb1 8:1 1 14.9G 0 part $ sudo mkfs.vfat -F32 /dev/sdb1 mkfs.fat 4.1 (2017-01-24) mkfs.vfat: failed whilst writing FAT
Confirm the new changes:
$ lsblk -o label,fstype,uuid /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT LABEL FSTYPE UUID sdb 8:0 1 14.9G 0 disk └─sdb1 8:1 1 14.9G 0 part vfat A6B5-97C8
Check if you can mount the device.
$ mkdir ~/mnt $ sudo mount /dev/sdb1 ~/mnt $ df -hT ~/mnt Filesystem Type Size Used Avail Use% Mounted on /dev/sdb1 vfat 15G 4.5G 11G 31% /var/home/jmutai/mnt $ sudo umount ~/mnt
Doing it non interactively
We can run all the commands in the terminal without invoking the parted interactive screen.
sudo parted -s -a optimal -- /dev/sdb mklabel msdos
sudo parted -s -a optimal -- /dev/sdb mkpart primary fat32 1MiB 100%
sudo parted -s -- /dev/sdb align-check optimal 1
sudo mkfs.vfat -F32 /dev/sdb1
That’s how to easily format SD Card, USB Drive, Flash Drive on Linux terminal as well as creating your desired filesystem type in the device. You can now remove the device and use it anywhere where the written filesystem is supported.
Related guides:
How to extend root filesystem using LVM on Linux
How To resize an ext2/3/4 and XFS root partition without LVM
Mount /tmp on a separate partition in Linux
How to create disk partitions in Windows using diskpart command