How to Format USB Drives and SD Cards on Linux

[ad_1]

Before you can use an SD card or USB drive, it needs to be formatted and partitioned. Typically most USB drives and SD cards come preformatted using the FAT file system and do not need to be formatted out of the box. However, in some cases, you may need to format the drive.

In Linux, you can use a graphical tool like GParted or command-line tools such as fdisk
or parted to format the drive and create the required partitions.

This article explains how to format a USB Drive or SD Card on Linux using the parted utility.

It’s important to note that formatting is a destructive process, and it will erase all the existing data. If you have data on the UDB drive or the SD card, make sure you back it up.

Installing parted #

GNU Parted is a tool for creating and managing partition tables. The parted package is pre-installed on most Linux distros nowadays. You can check if it is installed on your system by typing:

parted --version
parted (GNU parted) 3.2
Copyright (C) 2014 Free Software Foundation, Inc.
...

If parted is not installed on your system, you can install it using your distribution package manager.

Install parted on Ubuntu and Debian #

sudo apt updatesudo apt install parted

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 find the device name using the lsblk command:

lsblk

The command will print a list of all available block devices:

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
...

In the example above, the name of the SD device is /dev/sdb, but this may vary on your system.

You can also use the dmesg command to find the device name:

lsblk

Once you attach the device, dmesg
will show the device name:

...
[  +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) #

Before formatting the drive, you can securely wipe out all the data on it by overwriting the entire drive with random data. This ensures that the data cannot be recovered by any data recovery tool.

You need to completely wipe the data only if the device is going to be given away. Otherwise, you can skip this step.

Be very careful before running the following command and irrevocably erase the drive data. The of=... part of the dd command must point to the target drive.

sudo dd if=/dev/zero of=/dev/sdb bs=4096 status=progress

Depending on the size of the drive, the process will take some time to complete.

Once the disk is erased, the dd command will print “No space left on device”:

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 #

The most common file systems are exFAT and NTFS on Windows, EXT4 on Linux, and FAT32, which can be used on all operating systems.

We will show you how to format your USB drive or SD card to FAT32 or EXT4. Use EXT4 if you intend to use the drive only on Linux systems, otherwise format it with FAT32. A single partition is sufficient for most use cases.

Format with FAT32 #

First, create the partition table by running the following command:

sudo parted /dev/sdb --script -- mklabel msdos

Create a Fat32 partition that takes the whole space:

sudo parted /dev/sdb --script -- mkpart primary fat32 1MiB 100%

Format the boot partition to FAT32:

sudo mkfs.vfat -F32 /dev/sdb1
mkfs.fat 4.1 (2017-01-24)

Once done, use the command below to print the partition table and verify that everything is set up correctly:

sudo parted /dev/sdb --script print

The output should look something like this:

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 all! You have formatted your device.

Format with EXT4 #

Create a GPT partition table by issuing:

sudo parted /dev/sdb --script -- mklabel gpt

Run the following command to create a EXT4 partition that takes the whole space:

sudo parted /dev/sdb --script -- mkpart primary ext4 0% 100%

Format the partition to ext4:

sudo mkfs.ext4 -F /dev/sdb1
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   

Verify it by printing the partition table:

sudo parted /dev/sdb --script print

The output should look something like this:

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  

Conclusion #

Formatting a USB drive or SD card on Linux is a pretty straightforward process. All you need to do is insert the drive, create a partition table, and format it with FAT32 or your preferred file system.

If you hit a problem or have feedback, leave a comment below.

[ad_2]

Source link

Related Posts

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.