How to use the command e2fsck (with examples)
- Linux
- December 25, 2023
This article will guide you on how to use the e2fsck
command in Linux, with various examples illustrating its different use cases.
Command Description:
The e2fsck
command is used to check and repair a Linux ext2/ext3/ext4 filesystem. It is necessary for the partition to be unmounted before running this command. The e2fsck
command helps in identifying and fixing filesystem inconsistencies, damaged blocks, as well as performing various tests related to bad blocks.
More information: https://manned.org/e2fsck
Use case 1: Check filesystem, reporting any damaged blocks
Code:
sudo e2fsck /dev/sdXN
Motivation:
Running this command is useful when you suspect that the filesystem may have damaged blocks, and you want to identify and report them. It helps to ensure the overall integrity of the filesystem.
Explanation:
sudo
: Executes the command with administrative privileges.e2fsck
: The command name itself./dev/sdXN
: Specifies the device and partition of the filesystem to check.
Example output:
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdXN: X/Y files (Z/Z blocks)
Use case 2: Check filesystem and automatically repair any damaged blocks
Code:
sudo e2fsck -p /dev/sdXN
Motivation:
This command is useful when you want the damaged blocks detected by e2fsck
to be automatically repaired. It saves you from manually having to fix the damaged blocks.
Explanation:
sudo
: Executes the command with administrative privileges.e2fsck
: The command name itself.-p
: Automatically repairs any damaged blocks that are found./dev/sdXN
: Specifies the device and partition of the filesystem to check.
Example output:
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdXN: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdXN: X/Y files (Z/Z blocks)
Use case 3: Check filesystem in read-only mode
Code:
sudo e2fsck -c /dev/sdXN
Motivation:
Running the e2fsck
command in read-only mode is beneficial when you only want to check the filesystem for errors without making any changes. It helps in diagnosing potential issues without risking any accidental modification.
Explanation:
sudo
: Executes the command with administrative privileges.e2fsck
: The command name itself.-c
: Check for bad blocks on the filesystem only./dev/sdXN
: Specifies the device and partition of the filesystem to check.
Example output:
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdXN: clean, X/Y files (Z/Z blocks)
Use case 4: Perform an exhaustive, non-destructive read-write test for bad blocks and blacklist them
Code:
sudo e2fsck -fccky /dev/sdXN
Motivation:
This use case is applicable when you want to perform a comprehensive test for bad blocks on the filesystem. It tests the filesystem extensively in a non-destructive manner and provides an option to blacklist the detected bad blocks.
Explanation:
sudo
: Executes the command with administrative privileges.e2fsck
: The command name itself.-f
: Forcese2fsck
even if the filesystem is marked clean.-c
: Check for bad blocks on the filesystem only.-k
: Keep the bad blocks found during the check.-y
: Automatically answer yes to all questions asked bye2fsck
./dev/sdXN
: Specifies the device and partition of the filesystem to check.
Example output:
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
...
Pass N: Checking reference counts
/dev/sdXN: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdXN: X/Y files (Z/Z blocks), A/B bad blocks, C/Z spare blocks
Conclusion:
The e2fsck
command is a powerful tool for checking and repairing Linux ext2/ext3/ext4 filesystems. It helps to ensure the integrity of the filesystem by detecting and repairing damaged blocks, and performing various tests related to bad blocks. By understanding its different use cases, you can effectively maintain the health of your filesystems.