Your USB drive doesn’t show up when you plug it in. Worse, maybe Linux mounts it, but you can’t open a file; instead, you see the dreaded, incomprehensible I/O error message. We all know the sickening sensation when you think, “My USB drive is corrupted,” don’t we? I do. Good thing there’s hope! A corrupted USB drive isn’t a dead drive, and in my experience, Linux is one of the world’s greatest operating systems for fixing or restoring corrupt data from storage devices. This tutorial guides you step by step, from manually mounting the device to recovering lost data, using Linux commands to repair an error-ridden file system, clone an unreliable disk image, and recover lost files or even partitions.
Regardless if you’re a beginner just discovering this irritating issue on your newly purchased device, or an experienced Linux admin struggling with a broken file system on a server drive, this tutorial should equip you to get your storage device back up and running with or without your original data. Read on!
You'll need a USB stick (healthy or corrupted) to practice the mounting and repairing commands given below. So, grab one and get ready!
Understanding Why USB Drives Get Corrupted
Before sending commands to the drive, one must understand the underlying problem. Most USB drive corruption issues fall into one of the following categories:
- Filesystem corruption: The file system (i.e., FAT, journaling, or directory structure) is scrambled. Actual file data could be completely intact, but Linux doesn’t know how to access it.
- Partition table damage: The partition table at the beginning of the drive is messed up or completely overwritten, and Linux doesn’t know the partitioning layout.
- Bad sectors: There are physical defects on the NAND flash chip that can no longer hold data. This means it's hardware damage, and generally the worst kind.
- Bad ejection: If you're unplugging a USB drive while a write operation is going on, serious data corruption can happen. Linux buffers writes to make things go faster, and data might be sitting in RAM waiting to be written when you pull the plug.
- Power surges: Losing power during a write operation will leave the filesystem in a corrupted state.
- Age: Like all flash memory powered storage devices, USB's NAND flash memory has a limited number of write cycles. Older drives develop bad sectors and eventually fail.
Knowing which of these problems you're facing will decide the direction your entire recovery attempt should take.
Step 1: Identify Your USB Drive in Linux
The very first thing that you want to know is what Linux has called your USB drive. Do not assume it to be named /dev/sdb. It might be sdc, or sdd, and if you run repair commands without knowing the correct USB drive name, it can quite easily delete or corrupt important files on other drives.
Using lsblk
This is a useful tool which lists all of the block devices attached to your Linux system.
lsblk
Below is a possible example of the output that you might see.
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 476.9G 0 disk
├─sda1 8:1 0 512M 0 part /boot/efi
├─sda2 8:2 0 476G 0 part /
sdb 8:16 1 14.4G 1 disk
└─sdb1 8:17 1 14.4G 0 part
Note the RM in the third column; a value of 1 here means it is a removable device ( i.e., USB). In this example, sdb is the removable drive, and sdb1 is its single partition.
Using dmesg
When you insert a USB drive into the USB port, the system kernel registers a log of such events. To find the name that your system has given to the USB drive, insert your USB stick, and then run the following command.
dmesg | tail -20
And the output may look something like this:
[12345.678] usb 2-1: new high-speed USB device number 4 using xhci_hcd
[12345.901] sd 6:0:0:0: [sdb] 30277632 512-byte logical blocks
[12345.910] sd 6:0:0:0: [sdb] Attached SCSI removable disk
Here, you can clearly see the removable device is indeed sdb.
Using fdisk
The command fdisk has a useful -l switch that lists details of drives and their partitions, which is helpful in identifying the removable device.
sudo fdisk -l
A sample output may look like:
Disk /dev/sda: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: Samsung SSD 870 EVO 500GB
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 8A2F3C4D-5E6F-7A8B-9C0D-123456789ABC
Device Start End Sectors Size Type
/dev/sda1 2048 1050623 1048576 512M EFI System
/dev/sda2 1050624 1000214527 998164904 475.9G Linux filesystem
Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: SanDisk Ultra USB 3.0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7c4e5a9b
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 1953523711 1953521664 931.5G 7 HPFS/NTFS/exFAT
Disk /dev/loop0: 63.28 MiB, 66322432 bytes, 129536 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
You can see the third chunk of data in the example output. It clearly shows the USB drive and its name.
Note: Throughout this tutorial, I'll use the names /dev/sdb and /dev/sdb1 for the USB device. Make sure you are using the names discovered on your Linux system.
Step 2: Check the USB Drive's Mount Status
Before moving ahead, you have to ensure whether the drive/partition is currently mounted or not:
mount | grep sdb
Or, you can also use the following command:
lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT
If the USB drive is already in a mounted state and you want to repair it, first unmount it. Most repair tools won't operate on a mounted partition, as repairing an in-use filesystem is prone to causing more damage than fixing it.
Step 3: Manually Mounting a USB Drive
If for some reason your USB drive is not auto-mounted (often because of damaged/unsupported filesystems), here's how to do it.
Create a Mount Point
sudo mkdir -p /mnt/usb
Mount the Partition
sudo mount /dev/sdb1 /mnt/usb
In most USB drives, the filesystem is FAT32. In that case:
sudo mount -t vfat /dev/sdb1 /mnt/usb
In newer and larger USB drives (32GB+ capacity), the filesystem is exFAT:
sudo mount -t exfat /dev/sdb1 /mnt/usb
If you get the error message: "mount: unknown filesystem type 'exfat'", install its support as follows:
- On Ubuntu/Debian:
sudo apt install exfatprogs - On Fedora:
sudo dnf install exfatprogs - On Arch:
sudo pacman -S exfatprogs
For NTFS USB drives:
sudo mount -t ntfs-3g /dev/sdb1 /mnt/usb
Mount in Read-Only Mode
If the USB drive appears in bad health, mount it in read-only mode to stop further damage.
sudo mount -o ro /dev/sdb1 /mnt/usb
Troubleshooting "Structure needs cleaning" Error
If during the mount command, you receive this error message, this signifies that your filesystem was not properly unmounted and it needs to be repaired first. In that case, you will move to the next steps.
Step 4: Unmount Before Repair
Unmount the drive before you attempt to repair it.
sudo umount /dev/sdb1
If it's still in a mounted state, use the following command:
sudo umount -l /dev/sdb1 # Lazy unmount
You can also find out what's preventing it from unmounting:
lsof /mnt/usb
Fix that issue and fire the umount command again.
Step 5: fsck : The Swiss Army Knife of File-System Repairs
fsck (filesystem check) is the go-to tool in Linux for checking and repairing filesystems. It's quite similar to the chkdsk in Windows, but is way more powerful.
Basic Command Syntax
sudo fsck [options] /dev/sdb1
Repair a FAT32 USB Drive
sudo fsck.vfat -a /dev/sdb1
The -a flag is for automated repair. If you just want to see what the command will actually do, use the -n flag.
sudo fsck.vfat -n /dev/sdb1
And, if you are looking for a detailed checkup with a lot of verbose output, use the -v flag.
sudo fsck.vfat -v -a /dev/sdb1
Repair an NTFS USB Drive
Use the ntfsfix command for such USB drives:
sudo ntfsfix /dev/sdb1
Note: ntfsfix is not a full-fledged tool to repair all types of errors. It just makes your USB drive mountable if the corruption is not serious. For complete repair, run chkdsk /f in Windows to fix all types of NTFS issues.
Repair an ext4 USB Drive
To repair a USB Drive with an ext4 filesystem:
sudo fsck.ext4 -f /dev/sdb1
The -f flag enforces a check even if the drive appears in a clean state. You can also use the -y flag to automatically provide an affirmative (yes) response to all the repair prompts.
sudo fsck.ext4 -fy /dev/sdb1
Common fsck Exit Codes
| Exit Code | Meaning |
|---|---|
| 0 | No errors |
| 1 | Errors found and corrected |
| 2 | System reboot recommended |
| 4 | Errors left uncorrected |
| 8 | Operational error |
| 16 | Usage or syntax error |
| 32 | Cancelled by user |
| 128 | Shared library error |
A return code of either 0 or 1 signifies your USB drive is in good condition. A return code of 4 implies fsck was not able to repair the damage.
What to Do When fsck Keeps Finding Errors
If fsck keeps finding errors, you may have to run it multiple times before all the errors are resolved. You have to repeat this cycle until all the errors are fixed.
sudo fsck.vfat -a /dev/sdb1
# Rerun until the output shows no more errors.
Use the -a flag in this case.
Step 6: Use TestDisk for Partition Table Repair
If fdisk -l outputs nothing other than a raw block of device, then chances are, the partition table was wiped out, corrupt, or deleted. Luckily, that’s what we have testdisk for.
Install TestDisk
# On Ubuntu/Debian
sudo apt install testdisk
# On Fedora
sudo dnf install testdisk
# On Arch
sudo pacman -S testdisk
Run TestDisk
sudo testdisk /dev/sdb
textdisk is a command-line utility that interactively guides you through the partition table repair process.
- If you do not want logs, select the No Log option.
- Pick the drive in question from the list displayed by the tool.
- Select Intel as the partition table type, which is the most common type on USB drives.
- Select the Analyse option for partition scanning.
- Finally, select Quick Search to kickstart the process.
- If the utility manages to find the partition, press the P key to display files and to verify if data is recoverable.
- Choose Write to recover the partition table.
- Reboot and re-plug the USB drive to start using it again.
textdisk is a powerful utility that can reconstruct the entire partition table just by scanning and reading raw sectors.
Pro Tip: If Quick Search fails to recover the partition table, go for the Deeper Search option. It'll take more time, but chances of recovery are high.
Step 7: Recover Files with PhotoRec
The testdisk utility discussed in the previous section includes another powerful file recovery tool called photorec. The name indicates it's only for image files, but that's not the case.
It can recover almost any type of file by reading the raw sectors, bypassing the filesystem information. That's why it is effective in recovering data even when the filesystem is damaged significantly.
Run PhotoRec
sudo photorec /dev/sdb
- First, select the drive you want to recover the files from.
- Now, select the partition. If it's not visible, select the entire USB drive.
- Pick the type of filesystem on the drive. If you're unsure about it, simply select the Other option.
- Choose the directory where you want to save the recovered files. It should be some other drive and not the one you're recovering the files from.
- Finally, wait for the tool to scan the drive and recover the files.
Remember, all the recovered files will be stored in the numbered directories. And the names of the files will be different too. But don't panic because the content of each file will remain intact.
Step 8: Use ddrescue to Clone the Drive First
If you’re driving a dying vehicle, don’t punch the engine a couple times while trying to drive it. You’ll lose it entirely. Same with your hard drive: the worst thing you can do is to use fsck and testdisk on a drive that might be breaking apart. That further puts strain on a corrupted drive, and you may damage the very files that you will ultimately be salvaging.
Therefore, the best protocol is to clone first, and then attempt recovery from the clone.
ddrescue is a data recovery tool that recovers sectors from a drive as fast as possible and attempts to do a best-effort recovery when sectors are not readable at first. It’s made to be resilient and recover as much data as it can from bad drives while continuing the process intelligently by attempting retries.
Install ddrescue
# On Ubuntu/Debian
sudo apt install gddrescue
# On Fedora
sudo dnf install ddrescue
# On Arch
sudo pacman -S ddrescue
Clone the USB Drive
The storage capacity of the target drive where a clone will be made should be equal to or greater than the source drive.
sudo ddrescue -d -r3 /dev/sdb /dev/sdc recovery.log
Here's what each of the flags and parameters mean:
-d— Ignore OS cache and directly access disk sectors.-r3— Try to access bad sectors up to 3 times before moving ahead./dev/sdb— Source USB drive./dev/sdc— Destination drive where clone will be made.recovery.log— Progress tracking file that can be used to continue interrupted operation.
To continue with an interrupted session from here it left, use the following command:
sudo ddrescue -d -r3 /dev/sdb /dev/sdc recovery.log
ddrescue is intelligent enough to identify and use the old log file to resume the interrupted session. Once the clone has been created, you can repair it.
Clone to an Image File
Suppose you don't have a spare healthy USB drive to create a clone; you can create one in a file on the primary disk of your Linux system.
sudo ddrescue -d -r3 /dev/sdb ~/usb_recovery.img recovery.log
After the process is complete, mount the image file and repair it.
sudo losetup -f ~/usb_recovery.img
sudo losetup -l # Find the loop device, e.g., /dev/loop0
sudo fsck.vfat -a /dev/loop0p1
ddrescue is a powerful tool if used correctly.
Step 9: Handle "Read-Only Filesystem" Errors
One of the problems you may encounter while using a corrupted USB drive is that it is mounted in read-only mode after errors are detected by the Linux system. You may see an output like this:
[ 234.567] EXT4-fs error (device sdb1): ext4_journal_check_start:61: Detected aborted journal
[ 234.568] EXT4-fs (sdb1): Remounting filesystem read-only
Remount Forcefully in Read-Write Mode (Temporary Fix)
sudo mount -o remount,rw /dev/sdb1
Chances are that this attempt will fail as the drive has errors. A permanent fix is to unmount it and run the fsck command.
Repair and Remount the USB Drive
sudo umount /dev/sdb1
sudo fsck.ext4 -fy /dev/sdb1
sudo mount /dev/sdb1 /mnt/usb
Step 10: Check for Bad Sectors with badblocks
If the USB drive damage is more physical than just a filesystem corruption, badblocks can be used to scan the faulty sectors.
sudo badblocks -v /dev/sdb
This command executes a non-destructive read-only scan to identify the faulty sectors. For a thorough check, run a write test that will be destructive, erasing the data on the drive.
sudo badblocks -w /dev/sdb
Alert: The write test should only be executed if either all the recovery attempts have failed or you have recovered some of the data and further progress looks impossible.
If badblocks is detecting a large number of faulty sectors spread across the entire drive, it's better to replace it with a new USB drive.
Step 11: Format and Start Fresh
In case the data is not important for you and the filesystem corruption is beyond repair, it's always a good idea to wipe the drive and create a new filesystem on it for future use.
Format as FAT32
sudo mkfs.vfat -F 32 /dev/sdb1
Format as exFAT (Good for Large Files)
sudo mkfs.exfat /dev/sdb1
Format as ext4 (Linux Usage Only)
sudo mkfs.ext4 /dev/sdb1
Wipe the Partition Table First
If you want a complete wipeout and want to start afresh:
sudo wipefs -a /dev/sdb
sudo fdisk /dev/sdb
Now in the fdisk interface:
- Press the n key to create a new disk partition.
- For the partition covering the entire drive, accept the defaults.
- Use the t key if you want to change the filesystem type.
- Finally, press the w key to create the partition table.
Thereafter, you can format the USB drive as mentioned above.
Common Mistakes to Avoid
Rookies often make the mistake of running repair commands on a mounted filesystem. Always unmount the drive before you attempt to repair it.
Attempting repair or data recovery on a drive with physically damaged sectors is another big mistake. Use the ddrescue command to create a clone on which you can attempt the repair commands. This stops further damage to sectors on the original USB drive.
And, never attempt to save the recovered files on the same drive you are recovering from.
Intelligent users first analyse the drive's S.M.A.R.T. data before making a repair and recovery strategy. It gives you a glimpse into the drive's health and its history. Use the sudo smartctl -a /dev/sdb command to get this data. You'll need to install smartmontools first before firing this command.
If you want to stop the recovery process if partial data is all you're going to get, use the dd command instead of ddrescue.
Best Practices to Avoid USB Drive Corruption
Eject the drive correctly. Always use the sudo umount /dev/sdb1 command before removing the USB stick from the port. This ensures all the write buffers are flushed, and the data is transferred to the drive.
Handle write caching intelligently. If you frequently write a lot of data, use the sudo hdparm -W1 /dev/sdb caching mode to speed up the write process. Make sure to disable this mode with the -W0 flag after the write operation is complete.
Check the drive's health regularly. If data is important, check the drive's health with sudo fsck -n /dev/sdb1 to ensure you create a backup before the data becomes unrecoverable.
Use the drive's UUID in fstab. If you mount your USB drive every time your Linux system is booted, relying on a regular device name (e.g., /dev/sdb) is not recommended because it can change on subsequent boot. Instead, extract the drive's UUID through sudo blkid /dev/sdb1 and use that.
Conclusion
You might be sweating buckets after realizing your USB drive is corrupt and may think you have no chance to recover data. But knowing the underlying cause, whether it's a corrupted filesystem, damaged partition table, or just plain faulty hardware, is all you need. Once you are aware of the underlying cause, you only need to use the correct tools to repair your USB drive.
Start with low impact. Mount the drive and let fsck do its job. If not fixed, switch to more advanced tools depending on the diagnosis.
If the drive has bad sectors, first clone it with ddrescue before trying any other tool.
The USB repair capabilities offered by Linux through tools like fsck, testdisk, photorec, ddrescue, and badblocks are really superb. The vast number of corrupted drives out there that have been saved and their data brought back from the dead testify to their value.
Go slowly, step by step, and with any luck, your data will be returned safe and sound. Also, learn to disconnect (unplug) your USB device safely in the future.