How to Format USB Drives and SD Cards on Linux
Introduction
Before we begin talking about how to format USB drives and SD Cards on Linux, let's briefly understand – What is Formatting ?
Formatting prepares the drive for data storage by restructuring its file system, erasing existing data, and optimizing storage space. It enhances performance, resolves issues, and ensures compatibility across devices.
Most USB drives and SD cards are preformatted with the FAT file system and do not need to be formatted right away. In some cases, however, you may need to format the drive. To format the drive and create the necessary partitions on Linux, you can use a graphical tool like GParted or command-line tools like fdisk or parted. It is critical to understand that formatting is a destructive process that will erase all existing data. Make a backup of any data stored on the UDB drive or SD card.
In this tutorial, you will format USB drives and SD Cards on Linux. We will also address a few FAQs on how to format USB drives and SD Cards on Linux.
Advantages of formatting
- Optimize storage capacity: Formatting removes unnecessary files and reorganizes the drive, enhancing its storage capacity.
- Resolve errors: Formatting can fix common issues like corruption and read/write errors, improving drive reliability.
- Compatibility: A formatted USB drive and SD Cards ensures compatibility with different devices and operating systems.
- Enhanced performance: Formatting provides a fresh start, optimizing the drive's performance by removing fragmented data.
- Data protection: By erasing all existing data during formatting, it helps protect sensitive information from unauthorized access.
Installing parted
GNU Parted is a partition table creation and management utility. Most Linux distributions now come with the parted package pre-installed. You can see if it's installed on your computer by typing:
parted --version
Output
parted (GNU parted) 3.2
Copyright (C) 2014 Free Software Foundation, Inc.
...
If parted is not already installed on your system, use your distribution's package manager to install it.
Install parted
on Ubuntu and Debian
sudo apt install parted
sudo apt update
Install parted
on CentOS and Fedora
sudo yum install parted
Identifying the USB or SD Card Name
Insert the USB flash drive or SD card into your Linux machine and use the lsblk
command to determine the device name:
lsblk
The following command will output a list of all available block devices:
Output
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
...
sdb 8:16 1 14.4G 0 disk
└─sdb1 8:17 1 1.8G 0 part /media/data
...
The name of the SD device in the preceding example is /dev/sdb
, but this may differ depending on your system.
You can also find the device name using the dmesg
command:
lsblk
When you connect the device, dmesg
will display the device name:
Output
...
[ +0.000232] sd 1:0:0:0: [sdb] 30218842 512-byte logical blocks: (15.5 GB/14.4 GiB)
...
Securely Wipe Up the Data (Optional)
You can securely wipe out all the data on the drive before formatting it by overwriting the entire drive with random data. This ensures that no data recovery tool can recover the data.
You can skip this step if you want to.
Before running the following command and permanently erasing the drive data, exercise extreme caution. The target drive must be specified in the of=...
portion of the dd
command.
sudo dd if=/dev/zero of=/dev/sdb bs=4096 status=progress
The process will take some time depending on the size of the drive.
The dd
command will print "No space left on device" once the disc has been erased:
Output
15455776768 bytes (15 GB, 14 GiB) copied, 780 s, 19.8 MB/s
dd: error writing '/dev/sdb': No space left on device
3777356+0 records in
3777355+0 records out
15472047104 bytes (15 GB, 14 GiB) copied, 802.296 s, 19.3 MB/s
Creating a Partition and Formatting
On Windows, the most common file systems are exFAT and NTFS, on Linux, EXT4, and on all operating systems, FAT32.
We'll demonstrate how to format your SD card or USB device to FAT32 or EXT4. If you just want to use the drive with Linux systems, format it using EXT4. Otherwise, use FAT32. For the majority of use situations, one partition is adequate.
Format with FAT32
To begin, run the following command to create the partition table:
sudo parted /dev/sdb --script -- mklabel msdos
Make a Fat32 partition that takes up the entire space:
sudo parted /dev/sdb --script -- mkpart primary fat32 1MiB 100%
Format the boot partition to FAT32:
sudo mkfs.vfat -F32 /dev/sdb1
Output
mkfs.fat 4.1 (2017-01-24)
After that, run the following command to print the partition table and ensure that everything is in order:
sudo parted /dev/sdb --script print
The final result should look like this:
Output
Model: Kingston DataTraveler 3.0 (scsi)
Disk /dev/sdb: 15.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 15.5GB 15.5GB primary fat32 lba
That's it! Your device has been formatted.
Format with EXT4
Make a GPT partition table by issuing the command:
sudo parted /dev/sdb --script -- mklabel gpt
To create an EXT4 partition that takes up the entire space, use the following command:
sudo parted /dev/sdb --script -- mkpart primary ext4 0% 100%
Format the partition to ext4:
sudo mkfs.ext4 -F /dev/sdb1
Output
mke2fs 1.44.1 (24-Mar-2018)
/dev/sdb1 contains a vfat file system
Creating filesystem with 3777024 4k blocks and 944704 inodes
Filesystem UUID: 72231e0b-ddef-44c9-a35b-20e2fb655b1c
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
Print the partition table to confirm:
sudo parted /dev/sdb --script print
The final result should look like this:
Output
Model: Kingston DataTraveler 3.0 (scsi)
Disk /dev/sdb: 15.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 15.5GB 15.5GB ext4 primary
FAQs to Format USB Drives and SD Cards on Linux
Will formatting a USB drive or SD card erase all data?
Yes, formatting will erase all data on the drive or card. Make sure to back up any important files before proceeding with the formatting process.
Which file system should I choose when formatting?
The choice of file system depends on your requirements. For compatibility across different devices, FAT32 is widely supported. Ext4 is a common option for Linux-only usage, offering better performance and security features.
Can I format a USB drive or SD card to NTFS on Linux?
Yes, you can format a USB drive or SD card as NTFS on Linux by installing the NTFS-3g package. Use the 'mkfs.ntfs' command to format with NTFS file system, but be aware that NTFS write support on Linux is not as robust as on Windows.
How can I format a protected USB drive or SD card?
Some USB drives and SD cards come with write protection switches. Slide the switch to the unlocked position before formatting. If it is a software write protection issue, try using disk management tools to remove the write protection.
Can I format multiple partitions on a USB drive or SD card?
Yes, you can format multiple partitions on a USB drive or SD card. Use tools like 'fdisk' or 'parted' to manage and format the individual partitions.
What if the USB drive or SD card is not recognized or unable to be formatted?
If the drive or card is not recognized, check its connection and verify if the necessary drivers are installed. If formatting fails, the drive or card might be faulty or damaged, and it could be time to consider a replacement.
Is it possible to format a USB drive or SD card from the command line?
Yes, it is possible to format a USB drive or SD card from the command line on Linux. Tools like 'mkfs' or 'fdisk' provide command-line options to format and manage storage devices efficiently.
Conclusion
On Linux, formatting a USB drive or SD card is a simple process. Simply insert the drive, make a partition table, and format it with FAT32 or your preferred file system.
If you encounter a problem or have feedback, please leave a comment below, we'll be happy to respond to them.