Linux check the physical health of a USB stick [ Flash drive ]

How do I check the physical health of a USB stick in Linux operating systems? How do I check USB flash drive in Linux? Can you tell me Linux command to test and detect bad sector in USB flash memory or pen drives?

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.

ADVERTISEMENTS

Linux check USB stick or errors

The procedure to check the physical health of a USB pen drive in Linux:

  1. Open the terminal application.
  2. Insert your USB stick or pen drive into Linux system.
  3. To test and detect bad sector in USB flash memory or pen drive, run: badblocks -w -s -o error.log /dev/sdX
  4. 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:
lsblk
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

Check the physical health of a USB stick in Linux

Gif 01: Test usb stick health using badblocks command in Linux

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:
cat 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:

Download file using the wget command:
wget https://github.com/AltraMayor/f3/archive/v7.2.tar.gz
Untar tar ball on Linux, run:
tar xvf v7.2.tar.gz
Compile it:
make
Install it:
make install

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/:
f3write /mnt/
Linux check USB stick for health issues
Next, f3read will check if the flash disk contains exactly the written files:
f3read /mnt/
How to test my memory stick in Linux
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
Sample outputs:

[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

Conclusion

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS

Comments on this entry are closed.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.