dosfsck Command in Linux



The Linux dosfsck command checks and repairs the MS-DOS file system. dosfsck stands for DOS File System Consistency Check specifically designed to diagnose FAT or exFAT file systems.

The dosfsck command detects filesystem corruption and tries to repair it automatically. The repair process checks the boot sector, file allocation table, directory structure, and identifies the lost clusters.

Table of Contents

Here is a comprehensive guide to the options available with the dosfsck command −

Syntax of dosfsck Command

The general syntax of using the dosfsck command is as follows −

dosfsck [options] [device]

The [options] field is used to specify the various options to perform various operations. While [device] is used to mention the device name.

dosfsck Command Options

Options for the dosfsck command are listed in the table below −

Options Description
-a It automatically repairs the filesystem
-A It uses the Atari version of the MS-DOS file system. (Atari systems use a slightly different variation of the MS-DOS version)
-d It deletes the specified file (delete the first one if the file of the same name exists multiple times)
-f It saves the unused cluster chains of files
-n It checks errors non-interactively and does not perform any operation
-r It repairs the filesystem interactively
-t It marks the unreadable clusters as bad
-u It tries to undelete a specified file
-v It generates a detailed output
-V It performs a verification pass
-w It writes changes to disk immediately
-y It automatically repairs the filesystem (same as -a). If -y and -a are not specified, then the file system will only be checked, not repaired

Issues the dosfsck Command can Repair

The dosfsck command can repair the DOS filesystem issues in the following order −

  • Invalid Cluster − If the filesystem has any invalid cluster then it is changed to EOF
  • Filesystem Loops − If the filesystem contains a loop, then the loop is broken
  • Bad Clusters − The bad clusters are marked and removed (options)
  • Corrupt Directories − The corrupt directories can be deleted
  • Invalid Entries − File . and .. and other non-directories can be deleted or renamed
  • Root Directory Entries − Directories . and .. in the root are deleted or renamed
  • Bad File Names − Files with bad file names can be renamed
  • Duplicated Directories − If duplicate directories are present then they can be deleted or renamed
  • Directory Sizes − Directories with non-zero size can be set to zero to recognize the directory structure
  • Invalid Parent Pointer − If a directory does not point to its parent directory, then the start pointer is modified
  • Parent’s Parent Pointer − If the directory does not point to the parent of its parent directory then the start pointer is modified
  • Invalid Start Cluster − If the start cluster number of a file is invalid then the file is truncated
  • Bad or Free Sectors − If a file contains a bad or free sector, then the file is truncated
  • Cluster Chain Problems − If the cluster chain of the file is longer than indicated by the file size field then the file is truncated
  • Cluster Chain Length Mismatch − If the cluster chain of the file is shorter than indicated by the file size field then the file is truncated
  • Unused Clusters − If the cluster is marked as used but not occupied then it is marked as free

Examples of dosfsck Command in Linux

This section demonstrates the usage of the dosfsck command in Linux through various examples −

  • Checking and Repairing DOS Filesystem Automatically
  • Checking DOS Filesystem in Verbose
  • Checking DOS Filesystem for Errors Only
  • Listing Files being Processed

Note − The dosfsck command needs sudo privileges and the filesystem must be unmounted before checking and repair.

Checking and Repairing DOS Filesystem Automatically

To check and repair the DOS filesystem, use the -a option with the dosfsck command −

sudo dosfsck -a /dev/sda1 
Checking and Repairing DOS Filesystem Automatically

Checking DOS Filesystem in Verbose

The verbose output helps to determine the issue more precisely. To display verbose output, use the -v option.

sudo dosfsck -v -a /dev/sda1
Checking DOS Filesystem in Verbose

Checking DOS Filesystem for Errors Only

Before taking any action, it is important to identify errors only. To display the errors, use the -n option −

sudo dosfsck -n /dev/sda1
Checking DOS Filesystem for Errors Only

Listing Files being Processed

Listing the file under process can help to troubleshoot the filesystem problem more efficiently. To list all the files being processed, use the -l option −

sudo dosfsck -l /dev/sda1
Listing Files being Processed

Repairing DOS Filesystem Interactively

It’s better to repair a filesystem through a step-by-step process. It helps in avoiding any potential issues with the filesystem. To repair a filesystem interactively, use the -r option −

sudo dosfsck -r /dev/sda1
Repairing DOS Filesystem Interactively

Marking Unreadable Sector as Bad

To mark any bad sectors in a filesystem, use the -t option.

sudo dosfsck -v -t /dev/sda1
Marking Unreadable Sector as Bad

Note that this process may take time to complete.

Performing a Verification Pass

To check the filesystem integrity, the -V option is used.

sudo dosfsck -V /dev/sda1
Performing a Verification Pass

The verification-pass checks for inconsistencies and potential errors in the filesystem. If issues are discovered then it attempts to correct them.

Conclusion

The dosfsck command in Linux checks and repairs the DOS filesystem. This utility is similar to the fsck command in terms of functionality. It is a powerful utility used to identify the potential issues in the DOS filesystems and repair them.

In this tutorial, we explained in detail the dosfsck command, its syntax, and its usage in Linux through various examples.

Advertisements