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.
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.
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.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
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.
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.
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.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
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 Type | FAT (File Allocation Table) | NTFS (New Technology File System) | EXT (Extended File System) |
Use Case | Ideal for small storage devices (USB drives, memory cards) and legacy systems | Best for modern Windows systems with large storage and advanced features | Preferred for Linux systems, scalable for servers, and desktops |
Supported Operating Systems | Windows, macOS, Linux, various embedded systems | Windows, limited read-only support in macOS and Linux | Linux, limited support in Windows via third-party drivers |
Max File Size | 4 GB (FAT32) | 16 TB (with 64kB clusters) | 16 GB to 16 TB (EXT4) |
Security Features | Basic file attributes | File permissions, encryption, disk quotas, and journaling | File permissions, journaling (EXT3/4) |
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.
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.
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now
Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.
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.