Format SD card, USB drive, flash drive on Linux and separate them

As a person using operating systems, machines, and electronics, you usually need to format a Micro SD card, a USB drive / flash drive with a fat32 partition, or something similar. This could be writing OS data into it, copying files from one system to another, and using it as a portable storage medium. In this guide, we will look into how to format USB drives and SD cards on any Linux system using a split command line.

In the Linux operating system, there are graphics tools such as GParted and GNOME disks, which can be used in the GUI to format SD cards, USB drives, and flash drives. The focus of this guide is on using command line tools (partial). Other tools, such as fdisk, gdisk, cfdisk, sfdisk Can also be used.

Before you start

Before you begin, you need to install the GNU Parted application on your Linux system. GNU Parted is a program for creating and manipulating partition tables. Its front end is GParted.

Use the following shared command to install GNU Parted on your Linux machine.

--- 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 an SD card or USB / flash drive to your computer and confirm that the operating system can see it.

of lsblk Is a Linux command line tool that lists information about all available or specified block devices.

$ lsblk --all
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 is in / dev / sdb. After connecting the USB device to the computer, dmesg – Tools that print or control the kernel’s ring buffer will display connection information.

$ 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 and separate them

Make sure 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 the SD card and flash drive to be formatted.

$ lsblk --all

Before formatting a USB device, we will first erase the file system or partition table signature that may be present 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

Format SD card, USB drive, flash drive with Parted on Linux

Parted has two modes: command line and interactive. Breakups should always start with:

$ sudo parted device


  • device Is the SD card, USB drive or flash drive to be edited. If you omit the device parameter, parted will try to guess the device you want.

We will use interactive mode to simplify the partitioning process and reduce unnecessary duplication by automatically applying all partitioning commands to the specified device.

In my case, the command I would execute to start the operation on the 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.

You will notice that the command line prompt changes to (parted)

To see a list of available commands, enter:

(parted) help

Step 1: Create a new partitioned table

You need to create or recreate the partition table of the device-for the first partition, or change the type of its partition table.

Create a new Master Boot Record / MS-DOS Partition Table:

(parted) mklabel msdos


(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

For GPT partition tables, you will use;

(parted) mklabel gpt

Step 2: Create a partition on the device

You can determine the number and size of partitions your device should be divided into. The command syntax for creating a partition is:

(parted) mkpart part-type fs-type start end


  • Partial type Is one of them primary, extended Either logical– Only meaningful for MBR partitioned tables.
  • fs type Is an identifier used to set a 1-byte code that the bootloader uses to “preview” what kind of data is found in the partition and take appropriate action if necessary.
  • Start Is the start of the partition from the device.
  • End Is the end of the partition from the beginning of the device (not from the start value).

I will create a partition from 1 meter And ends with 100%.

(parted) mkpart primary fat32 1MiB 100%

Similar use cases include (Don’t run it-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 have 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 file system type of your choice.

I format the USB device partition as FAT32.

(parted) quit
Information: You may need to update /etc/fstab.

$ lsblk            
 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

sdb      8:0    1 14.9G  0 disk                         
└─sdb1   8:1    1 14.9G  0 part                  vfat   A6B5-97C8

Check if the device can be installed.

$ 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 


We can run all commands in the terminal without calling a separate 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

This is how to easily format SD cards, USB drives, flash drives on your Linux terminal and create the type of file system you need in your device. You can now remove the device and use it anywhere that supports a written file system.

Related guidelines:

How to extend the root file system with LVM on Linux

How to resize ext2 / 3/4 and XFS root partitions without LVM

Mount / tmp on a separate partition in Linux

How to use the diskpart command to create a disk partition in Windows

How to expand / increase KVM virtual machine (VM) disk size