Linux Commands

Format SD Card in Linux

An SD card must be formatted and partitioned before it can be used. Almost all SD cards come with the FAT file system that is already set up so they don’t need to be formatted the first time. Yet, there are times when formatting the drive is necessary.

You can format an SD drive and create the necessary partitions using a graphical tool like GParted or with command-line tools like fdisk or parted.

What Will We Talk About?

In this article, we will show you how to format an SD card on a Linux operating system. Specifically, we will use an Ubuntu 20.04 system for this article.

Prerequisite:

For this tutorial, you should have the following basic prerequisites:

  1. The user should have superuser privileges on the system.
  2. Basic knowledge of the Linux command line and partition management.

Warning: Formatting is a destructive process and deletes all data on the device. Make sure that you have a backup of important files that are stored on the SD card.

Looking for the SD Card Device

We need to find our target device on the system before we can move on. For this, run the following command before plugging in the SD card:

$ watch "dmesg | tail -10"

Now, we insert the SD card and watch the output of the previous command. It prints a message which confirms that the device is attached. We can also use the lsblk command to verify the device:

$ lsblk

A list of all block devices are printed by this command.

In our case, the name of the device is /dev/sdc. The name can be different depending on the distribution in use.

Unmounting the Device

There may be existing partitions on the target SD card. We can find them using the command:

$ ls -a /dev | grep sdc

If the output shows the device names with number suffixes, this means that there are formatted partitions. Again, please take a look at the previous warning in red.

Let’s unmount these partitions using the following command:

$ sudo umount /dev/sdc1

Approach 1: Using the Disk Utility to Format an SD Card

To format the SD card using the Disk Utility, follow the provided steps:

Step 1. Open the Disk Utility program from the Applications folder.

Step 2. From the pop-up window, select the target device from the list on the left. Now, from the right tab, select the partition which contains a filesystem. Then, left-click the gear icon and select the “Format Partition” option.

In order to make sure that no data remains on the device, you can also permanently delete the partition using the parted tool:

Step 3. Now, a new window appears where you can give a name to your SD card (volume). Also, if you want to overwrite all existing data, you can turn on the Erase switch.

For the “Type” block, you can select Ext4, FAT, NTFS, or any other available format. Now, press the Next button.

Step 4. Finally, click the Format button on the next window to start the process.

Your SD card is finally formatted and ready for use. You can mount it and start creating files and directories.

Approach 2: Using the Command Line to Format an SD Card

Let’s look at how to format an SD card from the command line.

Step 1. First, open the terminal and check the disk to be formatted:

$ sudo df -h

To complete the task, it is important to select the appropriate disk.

Step 2. Now, as we mentioned earlier in the “Unmounting the Device” section, unmount the SD card (skip this step if it is not mounted).

Step 3. Let’s create a new partition from the terminal with the GNU parted command. For this tutorial, we create a single partition that occupies the whole drive. The disk size in our case is 16 GB. The mklabel msdos parameter tells us that the standard for our partitions is MBR.

To use the GNU Parted command here, enter “parted” followed by the device filename for the target disk such as /dev/sdc.

$  sudo parted /dev/sdc mklabel msdos 0 16G

Step 4. In Linux, simply creating the partitions is not enough to render them useful. As a result, we must format them and create a file system for them. You can now format the SD card in any format that you need. For example, to use the ext4 format, we use the following command:

$ sudo mkfs -t ext4 /dev/sdc

Step 5. Next, mount the new partition on the filesystem, say /mnt. Also, verify using the following lsblk command:

$ sudo mount /dev/sdc /mnt

That’s all we have to do to format an SD card on Linux.

Conclusion

In this article, we looked at how to use the different file systems to format and mount an SD card disk in Linux. While formatting the disks under Linux is simple (assuming you know what you’re doing), any misstep might result in the loss of data.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.