What is the command to fix corrupted files?

There are a few common commands that can be used to attempt to fix corrupted files in Windows, Linux, and Mac operating systems. The specific command to use will depend on the type of corruption, the operating system, and whether the file system supports built-in repair utilities. However, some general troubleshooting techniques apply across operating systems and file systems.

Quick Fixes for Corrupted Files

Here are some quick answers to common questions about fixing corrupted files:

  • In Windows, the main command is chkdsk or Check Disk. This scans the drive and repairs errors.
  • On Linux, fsck is the command to check and repair file systems.
  • On Mac OS, Disk Utility can be used to verify and repair disk errors.
  • Corrupted system files may require reinstalling the operating system.
  • If a drive won’t mount, it may need to be formatted or replaced.
  • Backups allow restoring previous working versions of corrupted files.

While simple corruption can sometimes be repaired with basic commands, more serious file system or drive corruption requires advanced tools. Consulting technical help resources from operating system and hardware vendors is recommended for tricky cases of persistent corruption.

What Causes File Corruption?

Understanding what causes file corruption can help determine the appropriate solution. Common causes include:

  • Faulty hardware such as a failing hard drive or bad sectors.
  • Unexpected power loss or improper system shutdowns.
  • Buggy software, drivers, and filesystems with code defects.
  • Malware, viruses, ransomware, and hacked systems.
  • Full drives, fragmentation, metadata overwrites.
  • Copying errors, accidental deletion or modification.

Hardware issues tend to cause physical media errors that damage file contents. Software bugs and malware cause logical errors during reads and writes. Best practices like backups, disk health monitoring, and up-to-date antivirus help avoid corruption.

How to Check for File Corruption

Suspecting corruption is the first step. Warning signs include:

  • Programs crashing unexpectedly, often at launch.
  • Errors opening files or “file not found.”
  • Unreadable documents and media files.
  • Odd visual glitches or garbled data.
  • Unbootable drives and missing operating system files.
  • General system instability, crashes and freezes.

To confirm corruption, inspect files closely and run disk utilities. Potential techniques include:

  • Examining files in a text editor for glitches.
  • Using chkdsk or fsck to scan drives.
  • Verifying files against known good copies or checksums.
  • Monitoring SMART status of mechanical hard drives.
  • Testing memory with tools like memtest86+.

Advanced file recovery tools may help uncover corruption through deep inspection of drive contents at the raw binary level.

File Corruption on Windows

On Windows, the chkdsk utility included since DOS can check, detect bad sectors, and repair certain file system errors. To use it:

  1. Open Command Prompt as Administrator.
  2. Run chkdsk X: /f where X is the drive letter.
  3. Add /r to locate bad sectors if drive is failing.
  4. Add /x if drive cannot be locked to schedule check at next reboot.

Other Check Disk options like /i for indexes and /v for verbose output may help troubleshoot issues. chkdsk repairs only file system-level errors not corruption within file contents.

For system file corruption, Windows includes the System File Checker (SFC) tool to scan integrity and restore from cache:

  1. Open Command Prompt as Administrator.
  2. Run sfc /scannow
  3. Follow prompts to replace corrupted files from cache.

If SFC cannot repair the issues, DISM or in-place upgrade may be required to fix critical operating system files.

Third-Party File Repair Tools for Windows

When built-in tools are not enough, third-party file repair software for Windows provides more options such as:

  • Stellar Phoenix Windows Data Recovery – repairs documents, media, and photos.
  • EaseUS Data Recovery Wizard – recovers lost files and repairs corruption.
  • Wondershare Recoverit – deep scans to repair and restore damaged files.

Such tools go beyond chkdsk and SFC capabilities to directly analyze file contents and repair damage where possible. However, prevention through backups remains the best strategy.

File Corruption on Linux

The Linux equivalents to chkdsk and SFC are fsck and the package manager.

fsck checks and repairs common file system issues on Ext2/Ext3/Ext4 volumes:

  1. Unmount corrupted filesystem.
  2. Run fsck -y /dev/sdX1 (replace sdX1).
  3. Say yes to let fsck attempt repairs.
  4. Reboot once repairs complete.

For application files, package managers like apt, yum, pacman, etc. can verify and reinstall packages to fix missing or corrupt files. For example:

  • sudo apt-get install --reinstall [package]
  • yum reinstall [package]

Advanced file recovery tools also exist for Linux such as:

  • R-Studio – file recovery and disk repair utility.
  • PhotoRec – recovers photos, media, documents, and archives.
  • TestDisk – partition recovery and repair.

File Corruption on Mac

Mac OS includes Disk Utility for checking and repairing disks. To use:

  1. Open Disk Utility
  2. Select corrupted volume.
  3. Click First Aid tab.
  4. Click Run to begin repairs.

For applications, macOS has a built-in sfc-like tool called xartutil to verify and restore program files:

  1. Open Terminal
  2. Run xartutil --verify --arch x86_64 /Applications/App.app
  3. Run xartutil --extract --arch x86_64 /Applications/App.app to restore corrupted files

Third-party Mac file repair utilities also available:

  • DiskWarrior – repairs directories and file metadata.
  • Drive Genius – disk optimizer and repair tool.
  • Alsoft DiskWarrior – repairs disk directory damage.

Repairing Corrupted System Files

System file corruption is a serious issue that can prevent booting. Typical causes include:

  • Hard drive failures
  • Buggy OS updates
  • Virus infections
  • Power outages during writes

Repairing system files may require:

  • Using the built-in System File Checker.
  • Reinstalling the operating system or bootloader.
  • In-place upgrade/repair install.
  • Restoring from backups.

If system files are heavily corrupted, a clean OS reinstall or drive format may be unavoidable. Always maintain good backups.

Physical Hard Drive Corruption

With physical media errors, the drive hardware itself is failing. This prevents properly reading or writing data. Causes include:

  • Bad sectors from manufacturing defects or wear.
  • Mechanical failures of heads, platters, etc.
  • Electrical failures in control circuits.
  • Damage from drops, bumps, floods.

Signs of physical hard drive corruption:

  • Clicks, beeps, squeaks, and other odd noises.
  • Very slow performance.
  • S.M.A.R.T. errors reported.
  • BIOS not recognizing drive.
  • Drive not spinning up.

Repair options for physical drive failures are limited. Possibilities include:

  • Repairing drive electronics (for electrical failures).
  • Extracting platters and transplanting (for mechanical issues).
  • Using manufacturer low-level formatting tools.
  • Reading sectors with recovery tools to rescue data.
  • Replacing the failed drive.

If the drive is still somewhat operational, make copies of any important data right away. Consult a data recovery specialist for help.

Recovering Deleted Files

Accidentally deleted files are an easy mistake but recoverable in some cases. Actions to attempt:

  1. First, check the Recycle Bin or Trash – files may be there.
  2. Use file recovery software to scan and restore recently deleted files.
  3. If the file was on an SSD, stop writing data immediately.
  4. If file was on a mechanical drive, try removing the drive to read directly via adapters.
  5. Consult a data recovery company if needed for professional extraction.

The quicker you act, the better the chances. Overwriting data makes recovery nearly impossible on SSDs. On traditional hard drives, data sectors can sometimes be recovered even if overwritten.

Preventing File Corruption

While file corruption cannot always be prevented, certain best practices reduce the chances significantly:

  • Use disk monitoring tools to get alerted to potential drive failures.
  • Enable drive failure prediction technologies like S.M.A.R.T.
  • Replace drives before they fail based on age, health stats.
  • Use RAID or other redundancy for protection against failures.
  • Keep machines clean, cool, and on protected power to avoid electrical damage.
  • Shut down properly and use UEFI instead of “hard” power off.
  • Maintain at least 20% free space on drives.

Filesystem-level corruption is largely avoided by using Microsoft NTFS, Apple APFS, Linux ext4 and other journaling or checksum-enabled filesystems. Soft errors are logged and auto-corrected.

Backing up regularly provides protection by allowing restoration of previous intact versions of corrupted files.

When to Seek Professional Data Recovery

In cases of catastrophic drive failure or highly valuable and irreplaceable data, a professional data recovery service may be justified. Consider when:

  • DIY efforts have failed completely.
  • Cleanroom disassembly and advanced tools are needed.
  • Special proprietary repair methods may help.
  • Legal forensics require documented methodology.
  • Damage goes beyond software to mechanical failure.

Certified specialists have access to advanced equipment like platter transplants, PCB repairs, microscopy, disk imaging, and custom firmware hacks.

However, costs easily run $500+ and success is still not guaranteed. Business critical and personal data may merit the expense.

Conclusion

While no magic bullet exists to fix all file corruption, being prepared with the right tools and knowledge helps recover damaged files and minimize downtime. Awareness of common corruption causes, repair techniques for each operating system, smart backup diligence, and monitoring drive health are key preventatives.

For severe cases, technical assistance from OS and hardware vendors along with professional data recovery services provide hope when all else fails. But regular backups remain the top strategy for combating unexpected file and disk errors.