USB pen or stick is popular removable storage media. Like every other computer part, it will wear out with time. USB flash drive may develop bad sectors. Sometimes you buy a brand new pen drive and find out that it is not working. This page explains how to check the health status of a USB stick in Linux and obtain status so that you know if it is beyond repair or not.
Linux check USB stick or errors
The procedure to check the physical health of a USB pen drive in Linux:
- Open the terminal application.
- Insert your USB stick or pen drive into Linux system.
- To test and detect bad sector in USB flash memory or pen drive, run: badblocks -w -s -o error.log /dev/sdX
- To error check USB flash drive, you can use the f3write and f3read commands, which is an alternative to h2testw app from Windows operating systems.
Let us see all commands and examples in details.
Linux command to check the physical health of a USB stick
First, find out your USB stick or flash drive name under Linux, run:
The output indicated that I am using /dev/sda for USB and /dev/nvme0n1 for NVme pci ssd.
Warning: This will destroy any previously stored data on your USB pen/stick. Make sure you choose correct USB device name under Linux.
Once inserted the USB pen/stick, run the following command to search a device for bad blocks:
sudo badblocks -w -s -o error.log /dev/sda
Where options are as follows:
- -w : Use write-mode test to scans for bad blocks by writing some patternson every block of the device, reading every block and comparing the contents.
- -s : Show the progress of the scan.
- -o error.log : Write the list of bad blocks to the error.log file in the current working directory.
Use the cat command to view error.log:
Say hello to f3 tool
From the project home page:
f3 is a simple tool that tests flash cards capacity and performance to see if they live up to claimed specifications.
F3 stands for Fight Flash Fraud, or Fight Fake Flash.
How to install f3 tool on Linux
First, make sure you have compilers installed and running on Linux. If not, see the following tutorilas:
- Ubuntu Linux Install GNU GCC Compiler and Development Environment
- Debian Linux Install GNU GCC Compiler and Development Environment
- CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
Download file using the wget command:
Untar tar ball on Linux, run:
tar xvf v7.2.tar.gz
Testing performance with f3read/f3write
Use the following two command. First, f3write will write large files to your mounted USB pen disk. For example, my /dev/sda is mounted at /mnt/:
Next, f3read will check if the flash disk contains exactly the written files:
Zero data lost indicate that my USB pen drive working fine.
Quick capacity tests with f3probe on Linux
If you believe you have bought a fake flash drive, try the following Linux commands. The f3probe command is the fastest drive test and suitable for large disks because it only writes what's necessary to test the drive. It operates directly on the (unmounted) block device and needs to be run as a privileged user (be careful with device names again as --destructive option deletes all data):
sudo ./f3probe --destructive --time-ops /dev/sdb
[sudo] password for vivek: F3 probe 7.2 Copyright (C) 2010 Digirati Internet LTDA. This is free software; see the source for copying conditions. WARNING: Probing normally takes from a few seconds to 15 minutes, but it can take longer. Please be patient. Good news: The device `/dev/sdb' is the real thing Device geometry: *Usable* size: 15.24 GB (31969278 blocks) Announced size: 15.24 GB (31969278 blocks) Module: 16.00 GB (2^34 Bytes) Approximate cache size: 0.00 Byte (0 blocks), need-reset=no Physical block size: 512.00 Byte (2^9 Bytes) Probe time: 5'16" Operation: total time / count = avg time Read: 1.95s / 4814 = 405us Write: 5'11" / 4192321 = 74us Reset: 1us / 1 = 1us
The outputs from the above indicate that I do not have fake usb drive.
Good news: The device `/dev/sdb' is the real thing
How to correct capacity to actual size for my USB stick in Linux
Run f3fix command to creates a partition that fits the actual size of the fake drive. Only use to correct size for the fake drive. Use f3probe's output to determine the parameters for i3fix:
sudo ./f3fix --last-sec=16477878 /dev/sdb
You learned how to check the health status of a USB stick in Linux and further learned how to find out the actual size of a USB pen drive in case you got a faked USB pen drive.