dosfsck command in Linux with Examples
dosfsck stands for DOS File System Consistency Check and is part of the dosfstools package in Unix-like operating systems. This command diagnoses and repairs MS-DOS filesystems (FAT12, FAT16, FAT32). Before running dosfsck, it is crucial to unmount the filesystem to avoid data corruption.
Syntax:
dosfsck [options] device
The most basic usage of dosfsck
simply requires specifying the device you want to check.
Basic Example:
dosfsck [-aAflrtvVwy] [-d path -d ...] [-u path -u ...] /dev/sdX1
Common Issues Resolved by 'dosfsck' Command
'dosfsck' command can be used to correct some below the mentioned problems:
- Invalid cluster numbers in the FAT are changed to EOF.
- Loops in a file's cluster chain are broken.
- Bad clusters are marked and removed from associated files or directories.
- Corrupted directories may be dropped.
- Invalid start clusters result in file truncation.
- Duplicate directories may be renamed or dropped.
These filesystem problems can also be detected but are not fixed by dosfsck command if:
- Invalid parameters detected in boot sector.
- . and .. entries not found in non-root directories.
Common Options Used with the 'dosfsck' command
Option | Description |
---|---|
-a | Automatically repair the filesystem. |
-A | Toggle Atari variation of the MS-DOS filesystem. |
-b | Perform a readonly check on the boot sector. |
-d | Delete the specified file. |
-f | Salvage unused cluster chains to files. Unused clusters are typically added to free disk space. |
-l | List path names of files being processed. |
-n | No-operation mode; check is performed without making any changes. |
-p | Automatically repair filesystem errors (similar to -a ). |
-r | Interactive mode; prompts user for decisions when there are multiple ways to fix an error. |
-t | Mark unreadable clusters as bad. |
-u | Try to undelete the specified file. |
-v | Verbose mode; shows more details about the repairs being made. |
-V | Perform a verification pass without making any changes. |
-w | Write changes to disk immediately after fixing errors. |
-y | Automatically answer 'yes' to all prompts (similar to -a) |
Exit Codes for dosfsck
Understanding the exit codes returned by dosfsck helps you determine the result of the command’s execution:
- 0: No recoverable errors detected.
- 1: Recoverable errors were detected and fixed.
- 2: Syntax or usage error occurred.
dosfsck Command Examples
Let us look at some of the examples of dosfsck command to better understand the concept.
1. Automatically repair the file system.
To automatically repair the filesystem on a FAT-formatted partition, use the -a option:
sudo dosfsck -a /dev/sdb1

2. Readonly boot sector check
To perform a readonly check of the boot sector, use the -b option
sudo dosfsck -b /dev/sdb1

3. List path names of files being processed
You can list the path names of files being processed by 'dosfsck' using the '-l' option:
sudo dosfsck -l /dev/sdb1

4. Verbose Check and Repair with Marking Bad Clusters
Verbose way of checking and repairing the filesystem non-interactively. The '-t' used to mark unreadable clusters as bad, this will make them unavailable for new files and directories.
sudo -v -a -t /dev/sdb1

5. Perform a Verification Pass
If you want to run 'dosfsck' in verification mode without making any changes to the filesystem, use the '-V' option:
sudo dosfsck -V /dev/sdb1

Conclusion
The 'dosfsck' command is a versatile and essential tool for maintaining FAT-formatted filesystems on Unix-like operating systems. It offers both automatic and interactive options to diagnose and repair a wide range of filesystem issues. By following the above mentioned examples, you can confidently manage and repair MS-DOS filesystems using dosfsck.