Repairing a Corrupted USB Drive on Linux: A Step-by-Step Guide

Repairing a Corrupted USB Drive on Linux: A Step-by-Step Guide

Flash drives have become essential tools for data transfer, providing users with a convenient way to move files. However, issues such as corruption and malfunction can occur, leading to frustrating situations. Fortunately, Linux users, particularly those on Ubuntu, have access to multiple built-in utilities that can help restore functionality to a faulty USB drive. This guide will walk you through the steps to effectively repair a corrupted flash drive on a Linux system.

To ensure the safety of your data, we will start by backing up your flash drive’s content. This precaution is important because attempts to fix the drive might inadvertently worsen the situation. By creating a backup, you can safeguard against permanent data loss.

Step 1: Create a Compressed Full Backup

Linux offers various backup solutions, but the most reliable method involves using the dd command in conjunction with gzip for compression.

Before proceeding with backup, check whether the issue lies with the drive itself. Plugging the USB into another port or different computer can help discern if the problem is hardware-related, such as a loose connection.

Once you’ve confirmed that the flash drive is indeed problematic, connect it to your computer. Open your terminal by pressing CTRL + Alt + T. Identify your USB device by executing the following command:

ls /dev/disk/by-id

Repair USB In Linux List Disks By Id

If you’re unsure of the device name, you can also run lsblk or sudo fdisk -l to locate it (look for something like /dev/sdb, not a specific partition like /dev/sdb1).

To create a backup of your flash drive, use this command to compress the backup:

sudo dd if=/dev/disk/by-id/YOUR_FLASH_DRIVE status=progress | gzip -c > /home/USERNAME/backups/BACKUP_NAME.img.gz

Repair USB In Linux DD Backup To Img Gz

Ensure that the path “/home/USERNAME/backups/” exists; you can create it using mkdir -p /home/USERNAME/backups. The if= parameter refers to your flash drive, while gzip handles the compression process.

If you need to restore the backup, simply reverse the command sequence and specify your flash drive as the output device:

sudo gzip -cd /home/USERNAME/backups/BACKUP_NAME.img.gz | sudo dd of=/dev/disk/by-id/YOUR_FLASH_DRIVE status=progress

Step 2: Repair Filesystem with FSCK

Having secured your data, the next step is to attempt repairs. You can utilize the fsck command, which is effective for addressing corrupt file blocks—often the root cause of unreadability and corruption.

For this step, you will be working with the specific partition of the USB drive. Identify it using this command:

ls /dev/disk/by-id/usb*

Then, run the fsck command using the following syntax:

sudo fsck -v -y /dev/disk/by-id/YOUR_FLASH_DRIVE-PARTITION-TO-CHECK

Repair USB In Linux FSCK Fix Disk
  • sudo fsck grants administrative privileges to the command.
  • -v provides verbose output during the operation.
  • -y instructs the tool to automatically correct any detected errors.
  • /dev/disk… indicates the specific partition subject to the check.

Step 3: Format USB Drive with FDISK/MKFS

If fsck fails to remedy the issue, the next step is to format the drive, effectively resetting it to a new condition.

Begin by removing any existing filesystem data and creating a fresh structure using fdisk:

sudo fdisk /dev/disk/by-id/YOUR_FLASH_DRIVE

Repair USB In Linux Run Fdisk On Disk

Press o and hit Enter to set up a new DOS partition table, ensuring maximum compatibility. Alternatively, press g to create a GPT partition table if you’re using a modern operating system.

After that, create a new partition by pressing n and then p for a primary partition. Follow the on-screen prompts to accept the defaults and allocate the whole drive for this new partition.

Repair USB In Linux Fdisk Create Partition

Finally, press w to write the changes to the USB drive and exit.

Repair USB In Linux Fdisk List Partitions

Formatting the Partition

Your new partition will lack a filesystem, making it unusable until formatted. Use one of the mkfs utilities available in modern Linux distributions to format the partition. To format as FAT32—which is widely compatible—execute:

sudo mkfs.fat -F 32 /dev/disk/by-id/YOUR_FLASH_DRIVE-PARTITION

Repair USB In Linux Mkfs Fat

For NTFS formatting, which is recommended for Windows compatibility, use:

sudo mkfs.ntfs /dev/disk/by-id/YOUR_FLASH_DRIVE-PARTITION

Or for EXT4 formatting suitable for Linux systems:

sudo mkfs.ext4 /dev/disk/by-id/YOUR_FLASH_DRIVE-PARTITION

Repair USB In Linux Mkfs Ntfs

Keep in mind that EXT4 is optimized for Linux but is not compatible with Windows or macOS without additional software.

After formatting, you can mount the USB drive and verify its functionality by using lsblk again to check for filesystem recognition and mountability.

Step 4: Utilizing GUI Tools for Repairing and Formatting

If command-line operations are not to your liking, consider using the Disks utility available in Ubuntu. This tool is user-friendly and simplifies the process of checking and formatting your USB drive.

To find it, open your applications menu and search for Disks. Once located, launch the app.

Repair USB In Linux Locate Disks

Select your USB drive from the left-side list and click the gear icon. Choose Repair Filesystem and follow the provided steps to conduit the repair process.

If the filesystem is corrupt beyond recovery, you can reformat the USB drive. Select the USB drive, click the gear icon again, and then select Format Partition.

Repair USB In Linux Disks Format Partition

When prompted, enter a name for your USB drive in the Volume Name field and select your desired filesystem option:

  • Ext4: Optimized for Linux.
  • NTFS: Best suited for Windows.
  • FAT: Accommodates various devices, including smartphones and gaming consoles.
Repair USB In Linux Disks Format Options

Be mindful of the Erase option. For a quick format, keep it disabled. For a comprehensive erase, enable it—this is advisable for drives suspected to harbor bad blocks.

Repair USB In Linux Disks Full Erase Enabled

Step 5: Repairing with GParted

If you’d rather use a graphical interface, GParted is an excellent alternative for managing disk partitions and repairing drives, offering advanced options compared to the default Disks utility.

To start, install GParted via the terminal with the following command:

sudo apt install gparted

After installation, launch GParted either from your application menu or by running gparted in the terminal, followed by your password when prompted.

Best Free Disk Partition Management Tools Gparted Overview

Choose your USB drive from the dropdown menu in the top-right corner. Be careful to select the correct drive to avoid unintentionally erasing data from your main system drive. If your flash drive is mounted, right-click it and select Unmount. Then, you can perform a filesystem check or entirely format the drive using the respective options.

Conclusion

With these steps, your corrupted USB drive can be restored and ready for use, effectively returning it to a like-new condition. Should you also wish to create a Linux Live USB drive, detailed instructions are available here.

Source & Images

Leave a Reply

Your email address will not be published. Required fields are marked *