Use blkdiscard
In short:
blkdiscard /dev/mmcblk0
Discussion
To quickly erase an entire SD card, you can use the blkdiscard
(8) command. This calls the Linux BLKDISCARD ioctl, which in turn passes CMD38, the same as SD Memory Card Formatter.
Interesting options
-s
, --secure
securely discard blocks. As has been pointed out elsewhere, a normal CMD38 will make some blocks appear empty, but leave others plainly visible due to garbage collection. Use -s
if you wish to be a little more secure and force garbage collected blocks to also be erased.
-z
, --zero
zero-fill blocks rather than discard.
Does not work over USB
Note that while this works on my laptop and Raspberry Pi, it would not work on a USB SD card reader. The difference is that kernel needs to have access to the low-level MMC subsystem, which USB abstracts away as a generic "mass storage" device. [I believe Microsoft Windows has the same limitation, but please correct me in the comments if I'm wrong.]
If you do not have a device that can speak directly to the low-level SD card, it is possible to build one out of an Arduino and run SDFormatter.ino.
Extraordinarily dangerous!
Since blkdiscard
must be run as root, it can easily destroy all of your data. You better be sure that you are pointing it at the right device and then double-check again. Use lsblk
and df
.
The manpage claims that, to be safe, it will only work if the drive isn't already mounted. That would be a nice safety feature, but it is not true as of this writing. The current implementation requires the -f
, --force
option if the drive is already formatted, which is (almost) always going to be the case since SD cards come pre-formatted. Unfortunately, using --force
means that all checks, including if the drive is in use, are disabled.
Hopefully someday Linux will allow blkdiscard to be run on removable devices without requiring root privileges which would make it much less dangerous.
Other uses
Besides quickly wiping out data for privacy, supposedly blkdiscard, similar to fstrim for an SSD, will improve wear-leveling and make some SD cards generally run a little bit faster.
dd
can be fast if you find the right blocksize. – Panki Jul 23 '20 at 13:56