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

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.

Want the best experience? Become a premium member for ad-free browsing, access exclusive content, and ask questions in our private forums. Your membership helps support the site!
Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

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.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
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

Join Our Community!

Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.

Learn more

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.

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.

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

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.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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

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

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.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Similar Posts