Marcio Cunha

Corrupted hard drive? How to recover data via CLI

Learn how to rescue data from damaged hard drives using robust Linux command-line tools like ddrescue without risking permanent loss.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Corrupted storage units require careful diagnosis before any write attempt to prevent permanent file loss.
  • The ddrescue tool intelligently handles bad sectors by prioritizing the extraction of healthy disk areas first.
  • Creating a bit-by-bit image ensures that recovery work happens in a safe environment without straining original hardware.
  • File system analysis tools help map lost partition tables after the physical block rescue is complete.
  • Maintaining automated backup copies remains the only foolproof strategy against catastrophic storage failures.

Why hard drives corrupt and how CLI saves the day

When a hard drive starts making clicking noises or failing to read files, panic usually sets in for anyone storing important memories or source code. However, graphical recovery interfaces often freeze when trying to open damaged drives, overloading the dying hardware. That is precisely where the Linux terminal and CLI mode step in as indispensable allies for engineers and enthusiasts alike. As seen in infrastructure practices like configuring unstable clusters with Kubernetes health checks, rigorous monitoring prevents tragic surprises. The secret lies in interacting directly with the kernel and focusing on raw data extraction without heavy intermediaries.

Understanding the risks of damaged hardware

Before typing any command, it is worth understanding what happens physically inside the hard drive enclosure. Bad sectors are magnetic areas on the disk surface that have lost the ability to retain electrical charge, generating read errors known as I/O errors. When the operating system tries to read these corrupted chunks repeatedly, the read head can suffer severe wear or permanently damage the magnetic media. In practice, this means that fewer desperate attempts to copy files through the graphical explorer yield a much higher chance of command-line recovery success.

Preparing the ground with a bit-by-bit image

The basic principle of any forensic data rescue is never to operate directly on the original sick disk. The correct procedure consists of cloning every existing block to an image file on a healthy, spacious unit. For this, the traditional 'dd' command usually fails because it freezes or aborts as soon as it hits the first unreadable sector. The modern and safe solution is called ddrescue, a Linux utility specifically designed to copy data from unstable media. It reads in two distinct phases: first rescuing easy and fast areas, leaving problematic sectors for last, thus minimizing the mechanical stress on the drive.

Step-by-step guide to using ddrescue on Linux

To execute the rescue using ddrescue, open the terminal in a Linux distribution and ensure you have administrative privileges. The first step involves identifying the name of the damaged device through commands like fdisk, ensuring you do not select the wrong disk by mistake. Suppose your damaged disk is sdb and the backup destination is a safe file on another hard drive named imagem.img, accompanied by a log file that stores progress. Type the following sequence in the terminal:

sudo apt update && sudo apt install gddrescue -lsudo ddrescue -d -r3 /dev/sdb /mnt/backup/imagem.img /mnt/backup/mapfile.log

In this instruction, the -d parameter enables direct disk access bypassing the operating system cache, while -r3 sets three retry attempts for stubborn sectors before temporarily giving up. The mapfile.log file is vital because if the machine shuts down or the drive freezes, you can resume the process exactly where you left off without losing previous progress.

Analyzing and mounting the rescued image

As soon as the bit-by-bit cloning process finishes, you will have a heavy image file containing all recovered bytes from the corrupted unit. The next challenge is transforming this raw file into an accessible file system for navigation and individual file rescue. To mount this image in the Linux file system without burning it to another physical disk, we use the losup command associated with partition mapping. In practice, this means creating a virtual loop device that simulates the presence of a real hard drive connected to the motherboard.

Recovering individual files and fixing tables

With the image mounted via the loop device, you can inspect the content using traditional partition check and repair tools. If the partition table is corrupted to the point where the system fails to recognize folders, utilities like TestDisk or Photorec can scan the image structure searching for known file signatures like PDFs, images, and documents. This methodical workflow ensures that even after a catastrophic hardware failure, the most valuable data is surgically and safely rescued.

Final Considerations

Recovering data from corrupted hard drives using the terminal requires patience, technical knowledge, and respect for hardware physical limits. Tools like ddrescue change the game by allowing rescue to happen in a non-destructive and intelligent way, prioritizing the integrity of what can still be saved. By adopting a preventive posture, combining automated backup routines with constant diagnostics, the risk of permanent data loss drops dramatically, shielding your projects against mechanical unforeseen events.