Almost everybody knows it's important to back up the information on a computer, but relatively few people think about it until they've had a hard drive die unexpectedly. Modern hard drives reduce the likelihood of that happening with built-in diagnostic software called Self-Monitoring, Analysis and Reporting Technology (SMART), which detects disk errors and early signs of failure and reports them to your OS. If that OS is Ubuntu, you see an alert on your screen that your drive may need attention. It's also possible you might start noticing errors even if the disk isn't reporting problems.
First Things First
If you even suspect a disk problem, the first thing to do is back up your personal files, your photos and anything else that would be difficult to replace. You might or might not get any additional warning before the drive fails, and it's entirely possible to lose some of your data even if you successfully repair your disk. Having a backup of your files, even if they're corrupted, keeps the door open to recovering them later.
Have Ubuntu Check the Disk for Errors
If your computer is still operating and booting normally, it's pretty simple to have Ubuntu check the disk for errors. Click the "Activities" overview in the top left corner of your screen and type "Disks" into the search bar. The Disks program reads the drive's self-diagnostic data and shows an assessment of the disk's health. Ordinarily, it says "Disk is OK," but "Pre-fail" and "Fail" are also possible. Even "OK" doesn't outright guarantee that your disk is fine, so if you have errors, you want to test the disk manually anyway. Look under the "Volumes" graphic, and you'll see a small cogwheel icon. Click that and choose "Check filesystem." Ubuntu checks the disk for errors and returns a message, either giving your drive a clean bill of health or reporting errors. If you have errors, the next step is to fix them.
Have Ubuntu Repair the Disk
Ubuntu can repair the file system easily. From the same screen, click the cogwheel icon again. This time, instead of "Check filesystem," choose "Repair filesystem." It may take some time depending on the size and speed of your drive and the number of errors Ubuntu finds, but eventually, you get a message to let you know whether the repair succeeded. If it has, you can continue using the drive. Any files too damaged for automatic repair are saved, usually in a folder called "lost+found," where you can try to recover them manually with advanced tools. If the repair fails, you have to format the disk to make it usable again, which erases all your data. If you have irreplaceable data on that drive, it's best to replace the drive and keep the old one intact so you can attempt advanced data recovery methods or hire a professional to do it.
Repair From a Live Disk
It's not usually necessary anymore to have a separate Ubuntu repair disk to boot from in emergencies because the main OS itself boots and runs from a USB drive or an optical disk. At most, you might need to enter your computer's BIOS settings and tell it to boot from your thumb drive or DVD instead of trying to read the problematic hard drive. Once the OS boots up, you can run the Disks program the same way you normally would.
Repair From the Command Line
It's often more efficient to do things from the command line in Linux, and there's a good argument for knowing a few basic command-line skills. Unlike OS X and Windows, you can have any number of desktop interfaces in Linux – even in Ubuntu alone – and they don't all have the same graphical programs to work with. If you learn a handful of key command-line techniques, you can use those with any Linux OS regardless of the desktop interface it uses. It's a bit more complicated, but also more universal.
The FSCK Command
In command-line Linux, you repair bad sectors with the fsck command, which is short for "file system consistency check." In Linux, drives are either mounted or not mounted – essentially, active or inactive – and you need to unmount a drive before you run fsck on it to prevent accidental damage. You unmount a drive manually by typing the unmount command, followed by the name of the drive or drives, for example, "unmount /dev/sdb" without the quotes. With the drive unmounted, you can now type the fsck command.
A Sample Repair
Fsck recognizes plenty of variables, and you should read through them, but on a typical system you might enter the command like this: "sudo fsck -AR -C -y." This tells it to check all the disk filesystems in your computer, attempt to repair them, provide status monitors if they're supported so that you know how you're progressing, and do the repairs interactively so that you have input into what's happening. The "sudo" portion at the beginning tells the OS to give you administrator or "superuser" privileges temporarily, so you'll be prompted for an admin password before it lets you attempt to repair the disk. When it's done, fsck gives you a code 0 if no errors were found, a 1 if errors were corrected, a 2 if the system should be rebooted, and a 4 if it was unable to correct all the found errors.
The information contained here applies to Ubuntu 18.04. Parts of it may not apply to other versions of Ubuntu or Ubuntu derivatives.