Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: December 28, 2023
There are different portable storage mediums, but few are as small and widespread as memory cards. Although they often require additional specific hardware, non-volatile flash memory cards can now hold a vast amount of data and reach relatively good transfer speeds. However, to use them, we first need to know how to prepare and attach such mediums to the operating system (OS).
In this tutorial, we explore memory cards and ways to work with them on a Linux system. First, we perform a general overview of memory cards. After that, we focus on perhaps the most widely used type of memory card. Finally, we go over the practical steps to find, format, and mount with a memory card reader.
We tested the code on Debian 12 (Bookworm) with GNU Bash 5.2.15. It should work in most POSIX-compliant environments unless otherwise specified.
There are many non-volatile flash memory card standards:
Some were mainly used with specific devices, such as the Sony Memory Stick (MS, MSD, MSPD) standard and their devices, such as the PlayStation and PlayStation Portable. Others had brief fame in terms of portable storage and then got embedded into devices, such as the Universal Flash Storage (UFS) standard. Still others remained with niche use cases.
In general, memory cards have multiple applications:
Although not the fastest, by far, the most used and available type of memory card adheres to the Secure Digital standard.
Developed by the SD Association, the SD standard was born from the MultiMediaCard. Initially, SD was a closed proprietary specification, with open-source driver development even being banned. However, wide adoption led to reverse engineering and many free drivers.
There are several formats for the SD card form factor:
Another name for the microSD physical format is T-Flash (TransFlash, TF).
Importantly, standard SD cards are more rugged and have a physical write protection switch.
Currently, there are at least four classes of SD cards:
+------+---------------------+-------------+
| | Name | Capacity |
+------+---------------------+-------------+
| SD | standard | <= 2GB |
| SDHC | [H]igh capacity | 2GB - 32GB |
| SDXC | e[X]tended capacity | 32GB - 2TB |
| SDUC | [U]ltra capacity | 2TB - 128TB |
+------+---------------------+-------------+
Notably, these aren’t strict requirements but general guidelines.
Many speed class scales exist for SD cards. As speeds grow, scales haven’t gotten extended, but instead, new classes get created, some overlapping with older ones:
+---------+-----------------------------------------------+
| | Speed |
| | Scale |
+---------+-------+-----------+-------------+-------------+
| Speed | Speed | UHS Speed | Video Speed | SD Express |
| | Class | Class | Class | Speed Class |
+---------+-------+-----------+-------------+-------------+
| 600MB/s | | | | E600 |
| 450MB/s | | | | E450 |
| 300MB/s | | | | E300 |
| 150MB/s | | | | E150 |
| 90MB/s | | | V90 | |
| 60MB/s | | | V60 | |
| 30MB/s | | 3 | V30 | |
| 10MB/s | 10 | 1 | V10 | |
| 6MB/s | 6 | | V6 | |
| 4MB/s | 4 | | | |
| 2MB/s | 2 | | | |
+---------+-------+-----------+-------------+-------------+
While card classes don’t define speeds, bigger-capacity cards usually support higher speeds at the very least due to the total transfer time of the entire card contents.
While any storage card can be formatted with any filesystem, FAT32 was the default for SD and SDHC cards until the introduction of SDXC, when exFAT became the new default:
Considering the filesystem expectation, let’s understand how to format and mount SD and memory cards in general.
Regardless of the memory card type, we first need to have the relevant hardware and drivers. This way, our device can physically connect with, read, and write the card.
Usually, card readers are plug-and-play, but some systems could require a specific setup outside the scope of this article.
Since memory cards and their readers are block devices, we can use the lsblk command to list them:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 666G 0 disk
├─sda1 8:1 0 600G 0 part /
├─sda2 8:2 0 10G 0 part /var
├─sda3 8:3 0 666M 0 part [SWAP]
├─sda4 8:4 0 799M 0 part /tmp
└─sda5 8:5 0 54G 0 part /home
mmcblk0 179:0 0 66.6G 0 disk
├─mmcblk0p1 179:1 0 666M 0 part
└─mmcblk0p2 179:2 0 66G 0 part
Here, we see a standard disk at sda along with a memory card disk at mmcblk0. Due to the number suffix of the card, partition numbers have the p prefix.
As with other devices, memory cards show up under the /dev block devices path, usually as mmcblk*. Despite the mmc prefix, this can be any card (reader) type.
Critically, formatting devices and partitions wipe out their contents.
As mentioned, due to its flexibility, exFAT is now usually preferred over FAT32 for all but the oldest devices. So, let’s format a memory card as exFAT:
$ mkfs.exfat /dev/mmcblk0p1
Here, we assume the mmcblk0p1 partition was already in place. If that’s not the case, we can repartition as necessary.
Either way, at this point, we should have a new empty exFAT partition on /dev/mmcblk0p1.
Mounting cards and their partitions doesn’t differ much from the usual way to mount as long as we have the main components prepared:
For example, let’s mount the partition we formatted earlier:
$ mkdir /mnt/sd1
$ mount --types exfat --source /dev/mmcblk0p1 --target /mnt/sd1
In this case, we mount the first memory card partition at /mnt/sd1, a directory we created via mkdir.
Although the filesystem might be deduced by the mount command, it’s usually a good idea to specify it explicitly.
Now, let’s see the same with FAT32:
$ mkdir /mnt/sd2
$ mount --types msdos --source /dev/mmcblk0p2 --target /mnt/sd2
Here, we assume /dev/mmcblk0p2 holds a FAT32 partition, so we can mount it at /mnt/sd2.
In this article, we talked about memory cards and ways to manipulate them.
In conclusion, regardless of the memory card type, most Linux systems are equipped to handle basic actions with such storage mediums as long as there’s a card reader present and properly configured.