{"id":154,"date":"2021-12-08T18:10:11","date_gmt":"2021-12-08T17:10:11","guid":{"rendered":"https:\/\/linuxundich.de\/en\/checking-usb-sticks-and-sd-cards-for-errors\/"},"modified":"2021-12-08T18:10:11","modified_gmt":"2021-12-08T17:10:11","slug":"checking-usb-sticks-and-sd-cards-for-errors","status":"publish","type":"post","link":"https:\/\/linuxundich.de\/en\/checking-usb-sticks-and-sd-cards-for-errors\/","title":{"rendered":"Checking USB Sticks and SD Cards for Errors"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Lately I&#8217;ve often had the problem that the lousy sticks kept piling up at the top of the heap, so they were always the first ones in my hand when I reached into the junk box. At the latest when writing larger amounts of data, such as the ISO image of a Linux distribution or a Raspberry Pi OS image for a Raspberry Pi, problems appear. The read\/write rate collapses, or more telling I\/O errors show up right away. To rule this out, I want to check all my USB sticks and SD memory cards for errors once and sort out defective media for good. On Linux, you can do that with built-in tools.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Checking USB sticks for errors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, you need to find out the device ID of the USB stick or memory card. So plug the device into your computer or insert the SD card into the card reader and type <code>lsblk<\/code> in the terminal. The command lists all storage devices connected to the computer. As a rule, you can identify the device ID by the size of the device. In my example, the system attaches my 8 GB USB stick as <code>sdc<\/code> (that is, <code>\/dev\/sdc<\/code> in full). If you&#8217;re unsure, unplug the device you want to check and run <code>lsblk<\/code> again. If <code>sdc<\/code> is missing from the list, you have the right ID. Alternatively, graphical tools such as GNOME Disks also show the ID.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>lsblk<\/strong>\n[...]\nsdc           8:32   1   7.5G  0 disk\n\u251c\u2500sdc1        8:33   1   2.9G  0 part \/run\/media\/toff\/Ubuntu 21.10 amd64\n\u251c\u2500sdc2        8:34   1   4.1M  0 part\n\u2514\u2500sdc3        8:35   1   300K  0 part\n[...]<\/pre>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;6ac599cb65fbf&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"6ac599cb65fbf\" class=\"wp-block-image size-full wp-lightbox-container\"><img decoding=\"async\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on--pointerdown=\"actions.preloadImage\" data-wp-on--pointerenter=\"actions.preloadImageWithDelay\" data-wp-on--pointerleave=\"actions.cancelPreload\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/linuxundich.de\/wp-content\/uploads\/2021\/12\/gnome-laufwerksverwaltung.png\" alt=\"GNOME Disks shows the 8 GB USB stick with its device ID \/dev\/sdc.\" style=\"width:100%\"\/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\tdata-wp-bind--aria-label=\"state.thisImage.triggerButtonAriaLabel\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.thisImage.buttonRight\"\n\t\t\tdata-wp-style--top=\"state.thisImage.buttonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><figcaption class=\"wp-element-caption\">GNOME&#8217;s built-in Disks utility also shows the device IDs of hard disks, SSDs, USB sticks and other storage devices attached to the system.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For the actual error check, the <code>badblocks<\/code> command from the <em>e2fsprogs<\/em> package comes into play, which is part of the standard installation of the usual distributions. Badblocks may refuse to start the check at first because the drive is supposedly in use by the system. This usually happens because the operating system mounts the device automatically as soon as you plug in the USB stick. On my system, clicking the eject icon in the file manager isn&#8217;t enough; I have to unmount the device ID of the drive and its partitions explicitly with <code>umount<\/code>.<\/p>\n\n\n\n<p class=\"has-luminous-vivid-orange-background-color has-background wp-block-paragraph\"><strong>Caution:<\/strong> I have to issue a warning at this point: when checking for errors, Badblocks overwrites all data and partitions on the specified device. So make absolutely sure you pass the right device ID to the command, and back up important data from the USB stick or SD memory card beforehand. If the worst happens, you won&#8217;t be able to recover the data after the check.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>sudo badblocks -wsv \/dev\/sdc<\/strong>\n\/dev\/sdc is apparently in use by the system; it's not safe to run badblocks!\n$ <strong>sudo umount \/dev\/sdc*<\/strong>\numount: \/dev\/sdc: not mounted.\numount: \/dev\/sdc2: not mounted.\numount: \/dev\/sdc3: not mounted.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When calling Badblocks, you pass the parameters <code>-wsv<\/code> and the device ID. The parameters tell the program to run a destructive write test (<code>-w<\/code>), show the progress (<code>-s<\/code>) and output more information in general (<code>-v<\/code>). The thorough test writes every block of the device four times in a row with different patterns (0xaa, 0x55, 0xff and 0x00). Depending on speed and capacity, you need to allow quite some time for this. Checking the USB 2.0 stick with 8 GB of capacity from my example took more than an hour.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>sudo badblocks -wsv \/dev\/sdc<\/strong>\nTesting with pattern 0xaa:   0.00% done, 0:00 elapsed. (0\/0\/0 errors) done\nReading and comparing: done\nTesting with pattern 0x55:   0.00% done, 23:47 elapsed. (0\/0\/0 errors) done\nReading and comparing: done\nTesting with pattern 0xff:   0.00% done, 47:33 elapsed. (0\/0\/0 errors) done\nReading and comparing: done\nTesting with pattern 0x00:   0.00% done, 1:11:26 elapsed. (0\/0\/0 errors) done\nReading and comparing: done<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Besides the destructive method, Badblocks supports a non-destructive read-write mode. You enable it with the <code>-n<\/code> parameter instead of <code>-w<\/code>. In this variant, Badblocks first backs up the data of the block being checked to RAM, then overwrites the block with random data and checks whether the data was written correctly. Finally, Badblocks writes the backup back to the device. At first glance, this routine seems faster because there is only one pass, but in practice backing up and restoring the existing data takes considerably more time. In my example, it took more than 2.5 hours instead of just over one hour.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>sudo badblocks -nsv \/dev\/sdc<\/strong>\nChecking for bad blocks in non-destructive read-write mode\nFrom block 0 to 7812607\nChecking for bad blocks (non-destructive read-write test)\nTesting with random pattern:  94.36% done, 2:35:18 elapsed. (0\/0\/0 errors)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Detecting fake USB sticks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The tool <a href=\"https:\/\/github.com\/AltraMayor\/f3\">F3<\/a>, short for Fight Flash Fraud, goes one step further. The open-source program thoroughly checks whether a storage device really has the capacity it claims to have. Unfortunately, this is still a relevant topic, especially if you buy super cheap deals online. To expose fake USB sticks or SD memory cards, install the program from your distribution&#8217;s repositories. Debian, Ubuntu, Fedora and others carry the command-line tool in their official repositories; the package is usually called <em>f3<\/em>. Arch Linux only has the program in the AUR, so you need an AUR helper to install it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">### Installation on Arch Linux or Manjaro:\n$ <strong>yay -S f3<\/strong>\n### Installation on Debian, Ubuntu or Raspberry Pi OS:\n$ <strong>sudo apt install f3<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The commands for checking USB storage are <code>f3write<\/code>, <code>f3read<\/code> and <code>f3probe<\/code>. You have to use the first two in combination, passing each the mount point of the device as an option. <code>f3write<\/code> keeps writing one-gigabyte files to the mounted device until it runs out of space. Then you use <code>f3read<\/code> to check whether the written data can really be read back. If the device is a fake USB stick or a manipulated SD memory card, <code>f3read<\/code> would report corrupted sectors. This check leaves all data on the device intact; you just have to delete the h2w files at the end, otherwise there&#8217;s no space left on the stick.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>f3write \/run\/media\/toff\/291E-6F9C<\/strong>\n[...]\nFree space: 3.72 GB\nCreating file 1.h2w ... OK!\nCreating file 2.h2w ... OK!\nCreating file 3.h2w ... OK!\nCreating file 4.h2w ... OK!\nFree space: 0.00 Byte\nAverage writing speed: 3.91 MB\/s\n$ <strong>f3read \/run\/media\/toff\/291E-6F9C<\/strong>\n[...]\n                  SECTORS      ok\/corrupted\/changed\/overwritten\nValidating file 1.h2w ... 2097152\/        0\/      0\/      0\nValidating file 2.h2w ... 2097152\/        0\/      0\/      0\nValidating file 3.h2w ... 2097152\/        0\/      0\/      0\nValidating file 4.h2w ... 1518736\/        0\/      0\/      0\n\n  Data OK: 3.72 GB (7810192 sectors)\nData LOST: 0.00 Byte (0 sectors)\n\t       Corrupted: 0.00 Byte (0 sectors)\n\tSlightly changed: 0.00 Byte (0 sectors)\n\t     Overwritten: 0.00 Byte (0 sectors)\nAverage reading speed: 14.73 MB\/s\n$ <strong>ls -al \/run\/media\/toff\/291E-6F9C<\/strong>\ntotal 3907596\ndrwxr-xr-x  3 toff toff       4096 Jan  1  1970 .\ndrwxr-x---+ 4 root root         80 Dec  7 16:27 ..\n-rw-r--r--  1 toff toff 1073741824 Dec  7 18:35 1.h2w\n-rw-r--r--  1 toff toff 1073741824 Dec  7 18:40 2.h2w\n-rw-r--r--  1 toff toff 1073741824 Dec  7 18:44 3.h2w\n-rw-r--r--  1 toff toff  777592832 Dec  7 18:47 4.h2w\n[...]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>f3probe --destructive<\/code> works a little faster. The <code>--destructive<\/code> option is optional, but it speeds things up considerably. In this test, F3 only writes the sectors it absolutely needs and doesn&#8217;t bother with backing up data. Since the probe accesses the hardware directly, you pass the device ID (here <code>\/dev\/sdg<\/code>) instead of the mount point. This way, the test with my 4 GB USB 2.0 stick takes only a little more than seven minutes. Keep in mind again, though, that this routine overwrites all data blocks on the device.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ <strong>f3probe --destructive --time-ops \/dev\/sdg<\/strong>\n[...]\nGood news: The device `\/dev\/sdg' is the real thing\n\nDevice geometry:\n\t         *Usable* size: 3.73 GB (7831552 blocks)\n\t        Announced size: 3.73 GB (7831552 blocks)\n\t                Module: 4.00 GB (2^32 Bytes)\n\tApproximate cache size: 0.00 Byte (0 blocks), need-reset=no\n\t   Physical block size: 512.00 Byte (2^9 Bytes)\n\nProbe time: 7'17\"\n Operation: total time \/ count = avg time\n      Read: 2.73s \/ 4812 = 568us\n     Write: 7'13\" \/ 3637313 = 119us\n     Reset: 1us \/ 1 = 1us<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both Badblocks and F3 help you analyze errors on storage devices. You always need to bring a bit of time for the check, though. A thorough analysis of an 8 GB USB stick can easily take two hours, especially if the data is to be preserved during the check and the stick only supports the slow USB 2.0 protocol. Afterward, however, you can assume that the USB stick or SD memory card really works and has the capacity printed on the label.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Unlike hard disks, USB sticks and SD memory cards have no mechanical parts that wear out. Even so, modern flash memory breaks too. With Badblocks and F3, you can check flash memory for errors and expose fakes.<\/p>\n","protected":false},"author":2,"featured_media":158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"lui_source_id":41978,"lui_source_hash":"da1c93f358170f8e4223f09c459c8525937b9b82ef3b77b93f13b940f0cde0dc","lui_source_translated":"2026-10-06","lui_source_reviewed":true,"lui_via_url":"","lui_via_label":"","lui_source_url":"","lui_source_label":"","footnotes":""},"categories":[2],"tags":[],"class_list":["post-154","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gnu-linux"],"_links":{"self":[{"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":0,"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/media\/158"}],"wp:attachment":[{"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxundich.de\/en\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}