How to Format an SD Card from a Linux Terminal (CLI)

If you click our links and make a purchase, we may earn an affiliate commission. Learn more

Have you ever formatted an SD Card? I bet almost all of you would answer yes to this. However, we mostly opt for applications (like Raspberry Pi Imager) when formatting an SD card. What if you only have a Linux OS with a terminal window (lite version)? Well, this is the tutorial for you!

An SD card from the Linux terminal can be formatted using two popular pre-installed tools: fdisk and mkfs. In addition, the name of the SD card should be identified and the SD card should be unmounted before the actual formatting process.

This tutorial will guide you through with an easy-to-follow, step-by-step guide and visual aids. By the end, you will be a master at formatting SD cards with a Linux terminal! So, let’s get started.

If you need help with Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your system. Click here to get it for free!

How to Format an SD Card

Although you probably already know, it is important to note that formatting an SD card is a process where you will lose all the existing data stored on it. So, if it is important, please take the appropriate steps to ensure you don’t lose anything important.

Here are the three main steps involved in formatting the SD Card from the Linux terminal:

  • Identifying the Correct SD Card Name
  • Unmounting the SD Card
  • Formatting the SD Card

So, let’s get started with this tutorial. All you need is terminal access to a physically accessible Linux-based system (so that you can mount the SD Card).

Figuring out the device name

After mounting the SD card, the first step is listing the available mounts as we don’t want to format our primary disk or any other partition with important data stored in it. To do this, simple one-line commands exist for both fdisk and lsblk packages.

Always Forgetting Linux Commands?
Grab This Cheat Sheet!
I've compiled the must-know commands in a simple PDF for quick reference.

Download now

FDisk command

Fdisk is one of the go-to packages for disk management from Linux terminals (full documentation here). It comes pre-installed with almost every Linux distribution available.

To be sure, start by verifying that it is available on your system by using the following command:
fdisk --version

You should see the following output:



Great! Now that we know that we have fdisk, let’s list all the available storage devices mounted to our systems (you might have to enter your password, so please do so if prompted):
sudo fdisk -l

This command will give you a lot of information. Don’t worry, we only need the device we are concerned with. In most cases, you should see it listed as “/dev/xxx” as shown below.



Once you have the disk name, you can move to the next step.

Prefer reading without ads and popups?
Members get an ad-free version of every guide, plus exclusive project support.
Join the Community | Sign In

Tip: Command lines can be a pain to memorize. I put the essential Linux commands on a printable cheat sheet so you don't have to keep googling them. You can grab the PDF here if you want to save some time.

lsblk command

Alternatively, we can check for all the available drives to figure out the name of our SD card as we did before with the lsblk command. It is one of my favorite commands to do so (just because it gives a clean output):
lsblk -f

Always Forgetting Linux Commands?
Grab This Cheat Sheet!
I've compiled the must-know commands in a simple PDF for quick reference.

Download now

This will give an easy-to-read and clean output. You can easily see that in my case, the sda device is the one I need to format.

In my case, the name of the device that I need to format is sda.

Unmounting the SD Card

Linux systems do not allow you to format mounted drives. So, before proceeding with the formatting process, you need to unmount the SD card. This is automatically done when using software with a graphical interface.

However, it can be easily done using a single terminal command:
sudo umount /dev/sda

Replace the device name with the name of your SD card. It may ask you to enter your system password to get the essential permissions to perform this task. Once the command has successfully been executed, we are ready to format our SD Card.

Formatting the SD Card

Always Forgetting Linux Commands?
Grab This Cheat Sheet!
I've compiled the must-know commands in a simple PDF for quick reference.

Download now

Well, this is the most important part; formatting the SD card. To do this, you need to choose a specific file system type in which you would like to format your card. Please refer to the Related Questions section for an overview of the different file system types.

Once you select the appropriate file system type, execute the corresponding command as shown below.

We will use the “mkfs” utility to perform the formatting task. Depending on the file system you choose, here are the commands you would need to execute:

# vFAT File System
sudo mkfs.vfat -n 'Raspberry' -I /dev/sda

# NTFS File System
sudo mkfs.ntfs -n 'Raspberry' -I /dev/sda

# EXT4 File System
sudo mkfs.ext4 -n 'Raspberry' -I /dev/sda



It may take a few seconds Well, that’s it! You now have a fully formatted, empty SD card ready to be used for your next project. This marks the end of this tutorial.

FAQs for this Tutorial

Which File System Format should I choose for my SD card?

Numerous file systems are available for storage devices and each has its particular use case. For this tutorial, I will give you a comparison of the three most popular file systems: FAT, NTFS and EXT.

File System TypeFAT (File Allocation Table)NTFS (New Technology File System)EXT (Extended File System)
Use CaseIdeal for small storage devices (USB drives, memory cards) and legacy systemsBest for modern Windows systems with large storage and advanced featuresPreferred for Linux systems, scalable for servers, and desktops
Supported Operating SystemsWindows, macOS, Linux, various embedded systemsWindows, limited read-only support in macOS and LinuxLinux, limited support in Windows via third-party drivers
Max File Size4 GB (FAT32)16 TB (with 64kB clusters)16 GB to 16 TB (EXT4)
Security FeaturesBasic file attributesFile permissions, encryption, disk quotas, and journalingFile permissions, journaling (EXT3/4)
File Format Comparison
Always Forgetting Linux Commands?
Grab This Cheat Sheet!
I've compiled the must-know commands in a simple PDF for quick reference.

Download now

So, choose the one that best suits your application.

What is “Secure Wipe” and can it be done through the terminal?

There are some critical applications where one has to make sure that the data that has been formatted cannot be recovered by any recovery tool. That is when a secure wipe is necessary. What the system essentially does is that it overwrites the entire drive with random data.

That way, even if a tool recovers the data, all it will find is the random data. To do this, you need to run the following command in the terminal:
sudo dd if=/dev/zero of=/dev/sda bs=4096 status=progress

The “of=<>” part should point to your SD card. Once the execution of this command is complete, you will be able to see that the mounted SD card will be full now.


🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Prefer videos over reading? The RaspberryTips Community members get exclusive video lessons every month. Join now and watch them all right away. Get instant access.

Conclusion

Great, now you know how to format an SD card from the Linux terminal itself. If you follow the steps listed above, you won’t face any problems while formatting your SD card. This is just a basic task that the fdisk utility can do.

Apart from formatting, you can create partitions within your SD card (like a separate boot partition). This is just one possible application. You can go through the official documentation of fdisk to know the full extent of its capabilities. You can even research alternatives like parted.

Whenever you're ready, here are other ways I can help you:

Master Linux Commands: Overwhelmed with Linux commands? This book is your essential guide to mastering the terminal. It includes practical tips, real-world examples, and a bonus cheat sheet to keep by your side.

The RaspberryTips Community: Need help with Linux or want to chat with people who actually get it? Join the RaspberryTips Community and get access to private forums, exclusive lessons, and direct support.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts