How to use the command `fsck` (with examples)
- Osx
- December 25, 2023
The fsck
command is used to check the integrity of a filesystem and, if needed, repair it. It should be run when the filesystem is unmounted. It is essentially a wrapper that calls specific filesystem repair tools based on the file system type. The command is available in macOS and can be useful in ensuring the health and stability of a filesystem.
Use case 1: Check filesystem /dev/sdX
, reporting any damaged blocks
Code:
fsck /dev/sdX
Motivation:
This use case is helpful when you want to check the filesystem for any damaged blocks on the device /dev/sdX
. By running this command, you can get an overview of the filesystem’s condition and identify any potential issues.
Explanation:
fsck
: The main command to check the filesystem integrity./dev/sdX
: The path to the filesystem device you want to check.
Example output:
fsck 1.45.6 (20-Mar-2020)
e2fsck 1.45.6 (20-Mar-2020)
/dev/sdX: clean, 100/131072 files, 25000/524288 blocks
In this example, fsck
checked the filesystem on /dev/sdX
and reported that the filesystem is clean. It also provides information about the number of files and blocks in the filesystem.
Use case 2: Check filesystem /dev/sdX
only if it is clean, reporting any damaged blocks and interactively letting the user choose to repair each one
Code:
fsck -f /dev/sdX
Motivation:
In this use case, the -f
flag is added to check the filesystem only if it is clean. If any damaged blocks are found, the command will prompt the user to choose whether to repair each one. This interactive mode allows the user to have more control over the repair process.
Explanation:
fsck
: The main command to check the filesystem integrity.-f
: Specifies that the filesystem should only be checked if it is clean./dev/sdX
: The path to the filesystem device you want to check.
Example output:
fsck 1.45.6 (20-Mar-2020)
e2fsck 1.45.6 (20-Mar-2020)
/dev/sdX: 1060/131072 files (0.2% non-contiguous), 12000/524288 blocks
The output shows that some files and blocks are damaged or non-contiguous. The command provides options to repair these damaged blocks.
Use case 3: Check filesystem /dev/sdX
only if it is clean, reporting any damaged blocks and automatically repairing them
Code:
fsck -fy /dev/sdX
Motivation:
In this use case, the -f
and -y
flags are combined to ensure that the filesystem is automatically repaired without any prompts. This can be useful when you want to perform an automatic repair without any user intervention.
Explanation:
fsck
: The main command to check the filesystem integrity.-f
: Specifies that the filesystem should only be checked if it is clean.-y
: Specifies that the command should automatically repair any damaged blocks without user prompts./dev/sdX
: The path to the filesystem device you want to check.
Example output:
fsck 1.45.6 (20-Mar-2020)
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/sdX: ***** FILE SYSTEM WAS MODIFIED *****
/dev/sdX: 1060/131072 files (0.2% non-contiguous), 12000/524288 blocks
The output illustrates various passes that fsck
goes through to check the filesystem. In this example, the command automatically repairs the damaged blocks. The “FILE SYSTEM WAS MODIFIED” message indicates that changes were made to fix the filesystem.
Use case 4: Check filesystem /dev/sdX
, reporting whether it has been cleanly unmounted
Code:
fsck -q /dev/sdX
Motivation:
This use case is useful when you want to determine whether a filesystem has been cleanly unmounted. The -q
flag is used to suppress unnecessary messages, providing a concise output specifically focused on the clean unmount status.
Explanation:
fsck
: The main command to check the filesystem integrity.-q
: Suppresses unnecessary messages, including the filesystem inspection details./dev/sdX
: The path to the filesystem device you want to check.
Example output:
Filesystem was not unmounted cleanly
The output indicates that the filesystem /dev/sdX
was not cleanly unmounted. This information is helpful in understanding the state of the filesystem and whether further actions, such as repair or remounting, are necessary.
Conclusion:
The fsck
command is a powerful tool for checking and repairing filesystem integrity issues. It provides various options to tailor the inspection and repair process according to specific needs. By using the examples provided, users can perform filesystem checks, interactively repair damaged blocks, or automatically repair known issues. Additionally, the command can validate whether a filesystem has been cleanly unmounted, providing insight into its current state.