Linux dosfsck Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux dosfsck command to check and repair errors on a FAT32 file system. The lab covers the purpose and syntax of the dosfsck command, as well as practical examples of checking and repairing a FAT32 file system on a USB drive. The steps include understanding the command, performing a thorough file system check and repair, and addressing any issues that may arise. This lab is designed to provide you with the necessary skills to maintain and troubleshoot FAT32 file systems on your Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") linux/SystemInformationandMonitoringGroup -.-> linux/mount("`File System Mounting`") subgraph Lab Skills linux/ls -.-> lab-422649{{"`Linux dosfsck Command with Practical Examples`"}} linux/rm -.-> lab-422649{{"`Linux dosfsck Command with Practical Examples`"}} linux/dd -.-> lab-422649{{"`Linux dosfsck Command with Practical Examples`"}} linux/mount -.-> lab-422649{{"`Linux dosfsck Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the dosfsck Command

In this step, you will learn about the purpose and syntax of the dosfsck command in Linux. The dosfsck command is used to check and repair errors on a FAT32 file system.

First, let's understand the purpose of the dosfsck command:

$ man dosfsck
dosfsck - check and repair DOS file systems

The dosfsck command is used to check and repair errors on a FAT32 file system. It can be used to fix various issues such as corrupted file system metadata, lost clusters, and other file system inconsistencies.

Now, let's look at the basic syntax of the dosfsck command:

$ dosfsck [options] <device>

Here's a breakdown of the command options:

  • -a: Automatically repair the file system.
  • -v: Verbose output, showing all actions taken.
  • -t: Test the file system without actually making any changes.
  • -r: Interactively repair the file system.
  • -l: List the root directory.
  • -L: List the contents of the file system.
  • -n: No-operation mode, just list errors.
  • -p: Automatically repair the file system without prompting.

Example usage:

$ sudo dosfsck -v /dev/sdb1

This command will perform a verbose check and repair on the FAT32 file system located on the /dev/sdb1 device.

Check and Repair Errors on a FAT32 File System

In this step, you will learn how to use the dosfsck command to check and repair errors on a FAT32 file system.

First, let's create a FAT32 file system on a USB drive:

$ sudo mkfs.vfat -F 32 /dev/sdb1

Now, let's intentionally corrupt the file system by creating a file with an invalid name:

$ sudo touch /media/labex/USB_DRIVE/invalid_file#@!.txt

Next, let's use the dosfsck command to check and repair the file system:

$ sudo dosfsck -a /dev/sdb1
dosfsck 4.2 (2021-01-31)
/dev/sdb1: 1 files, 1/2048 clusters
Reclaimed 1 unused cluster(s)

The -a option tells dosfsck to automatically repair the file system without prompting the user. The output shows that one unused cluster was reclaimed, indicating that the file system was successfully repaired.

To verify the repair, let's list the contents of the file system:

$ sudo dosfsck -l /dev/sdb1
Directory dump:
/           <DIR>   2048    0 Jan  1 1980
INVALID_FI~1 TXT       0    0 Jan  1 1980

The output shows that the invalid file has been removed, and the file system is now clean.

Perform a Thorough Filesystem Check and Repair on a USB Drive

In this step, you will learn how to perform a more thorough check and repair of a FAT32 file system on a USB drive.

First, let's insert a USB drive into the system and identify the device name:

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 119.2G  0 disk
└─sda1   8:1    0 119.2G  0 part /
sdb      8:16   1   7.5G  0 disk
└─sdb1   8:17   1   7.5G  0 part /media/labex/USB_DRIVE

In this example, the USB drive is /dev/sdb1.

Now, let's perform a thorough check and repair on the file system:

$ sudo dosfsck -a -v -w /dev/sdb1
dosfsck 4.2 (2021-01-31)
/dev/sdb1: 2 files, 4/1920 clusters
Reclaimed 2 unused cluster(s)

The options used here are:

  • -a: Automatically repair the file system.
  • -v: Verbose output, showing all actions taken.
  • -w: Write changes to the file system.

The output shows that two unused clusters were reclaimed, indicating that the file system was successfully repaired.

To verify the repair, let's list the contents of the file system:

$ sudo dosfsck -l /dev/sdb1
Directory dump:
/           <DIR>   2048    0 Jan  1 1980

The output shows that the file system is now clean and ready for use.

Summary

In this lab, you first learned about the purpose and syntax of the dosfsck command in Linux, which is used to check and repair errors on a FAT32 file system. You explored the various command options, such as automatically repairing the file system, verbose output, and testing the file system without making changes. You then demonstrated how to use the dosfsck command to check and repair errors on a FAT32 file system, including intentionally corrupting the file system and then using the dosfsck command to automatically repair it.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like