Marcio Cunha

Corrupted files? How to recover partitions with TestDisk

Lost access to your data? Learn how to use TestDisk to find lost partitions, fix partition tables, and restore critical files on Linux.

Marcio Cunha•2 min
Also available in:PortuguêsEspañol
Summary
  • The partition table is the index used by the operating system to locate data blocks on physical storage.
  • TestDisk operates at the low-level filesystem layer to identify partition signatures even after severe corruption.
  • Creating a forensic image of the disk before any repair attempt is the golden rule to prevent permanent data loss.
  • Data recovery tools perform best when the affected disk is not mounted as a system partition in read-write mode.
  • Hardware integrity verification, as explored in our guides on spotting I/O bottlenecks , should always precede software-based restoration.

What is TestDisk and when to use it

When you attempt to access a disk and the system returns read errors or 'not formatted' messages, the issue usually lies with the partition table, not your files. Think of this table as a book's index: it tells the operating system where every data block begins and ends. If the index is corrupted, the system loses access to the content. TestDisk is an open-source tool that analyzes the disk structure bit-by-bit to find lost or damaged tables.

Preparation: safety before recovery

Before running any tool, understand that a corrupted disk might be physically degrading. If the disk shows clicking noises or extreme slowness, running software tools might trigger a total failure. If possible, create a disk image using ddrescue before starting. This creates a bit-perfect copy in a single file, ensuring that if things get worse during the process, you still have the original version to try again.

Step-by-step: scanning and restoring tables

TestDisk works via the terminal but uses an interactive ncurses-based interface for easy navigation. To begin, install the testdisk package and run it as root.

  1. Launch the program with sudo testdisk and select 'Create' to log your actions.
  2. Choose the affected disk from the detected list. Be extremely careful to identify the correct device by size and model name.
  3. Select the partition table type (usually 'Intel/PC' for older BIOS systems or 'EFI GPT' for modern hardware).
  4. Choose 'Analyse' and then 'Quick Search' to have the program scan the disk geometry for lost partitions.
  5. If TestDisk finds your partitions, highlight the correct one and press 'Write' to save the restored partition table to the disk's start sector.

Recovering specific files from damaged partitions

Sometimes a partition cannot be fully repaired, yet the files remain intact. Within TestDisk, after listing folders with the 'List' option, you can browse your directories as if you were in a standard terminal. If you find the required documents, select them and press 'c' to copy them to a secure directory on a different disk. This is the most efficient way to save critical data without needing to rebuild the entire filesystem. To complement this implementation in production environments, see our related technical analyses on Monitor has no light? How to test backlight with multimeter and How to convert and resize videos via terminal with FFmpeg.

Final thoughts on integrity

Data recovery is a delicate procedure. After restoring access, move your data to a new device immediately and do not trust that disk for primary storage anymore. Keep in mind that software failures can mask hardware issues, and validating disk health by checking for read errors is just as important as auditing file permissions and states after restoration. Prevention, through regular backups, remains the only foolproof strategy.