Linux: Find and fix USB stick (or other disk / device) errors

If you are having repeated errors writing to a USB stick, then it might need to be checked for errors and have all the bad blocks marked. Do this with the badblocks command in Linux. (Unmount the device first.) For example, if the USB is at /dev/sdd1:
#sudo badblocks -n -s -v -o badsectors.txt /dev/sdd1

-n use non-destructive read / write mode (better than just reading)
-s show progress on the screen (otherwise, you're wondering it it is doing anything for a long time)
-o  write out the baddies to a text file that can be used as input to a fixer like fsck
Before doing this, the device is found using the df -l command, and unmounted using the sudo umount command. After this, use the badsectors.txt file to fix and mark all of the bad blocks. For example,
#sudo fsck -l badsectors.txt /dev/sdd1
11 / 2021