Filesystem Consistency Check

What is fsck?

fsck is a command you can use to check your filesystem. The command will invoke one of the following: fsck_hfs, fsck_msdos, fsck_exfat, and fsck_udf, depending on your filesystem type. If the filesystem type is not one of these, fsck will return an error.

Note, fsck will most likely invoke fsck_hfs (if you are using OSX), as does Disk Repair. They essentially do the same thing, both running the same “program.” If you are on your computer, in your operating system, use Disk Repair, not fsck. Improper use of fsck can cause serious issues if you do get it to run. Don’t just try to open up terminal and start using this command.

You are going to get this warning a few times throughout this article: do not use fsck on a mounted filesystem.

What does fsck do?

 

This command looks for and interactively repairs filesystem inconsistencies. Often, fsck is used in OSX when the operating system is having issues (such as failure to load the operating system).

How do you use fsck?

You never use fsck on a mounted drive, as it can cause a lot of problems. This makes sense, you don’t want to try to repair inconsistencies on a filesystem that you are currently using. This is why using fsck in Single User Mode (whether you can load your OS or not) is often recommended, your filesystem is not mounted as read-write.

To boot into Single User Mode, hold down Command+S when your computer is turning on. You should see a black screen with white text load up.


There are a few flags that are commonly used with fsck, and any flags given to fsck will be passed to the appropriate command (again, most likely fsck_hfs):

-y will answer all questions that fsck would ask with “yes”

-f will force fsck to check filesystems it might usually pass over, depending on what type of file system you are checking

-q will do a quick check to see if the file system was unmounted cleanly

fsck will check all your file systems. In Single User Mode, you can simply type fsck -fy to run through and check/repair your filesystems.


Advanced fsck: fsck_hfs

If you’re on OSX, you are most likely invoking fsck_hfs when you use fsck. This checks HFS and HFS+ filesystems. You can use the command fsck_hfs directly if you want. You can’t use fsck in any form on a mounted disk if you want to actively repair it.

The flag -f has the following specific meaning for fsck_hfs:

-f will force fsck to check and repair Journaled HFS+ file systems


There are a couple other flags for fsck_hfs that can be helpful as well:

-d displays debugging information. this is useful if fsck is encountering error.

-l locks down the filesystem and fsck does not attempt any repairs, simply checks the filesystem. using this flag, you can check a mounted filesystem. again, no repairs will be attempted.

-E will cause fsck to exit upon a major error (an error that would impact normal usage of the Volume)

-y always repair inconsistencies

-n never repair inconsistencies


There’s another useful flag you can use, but it is a little different than flags we have seen before. To use this flag, you type the flag, followed by a space, and then a certain value.

-D flags displays additional debugging information. Two “flags” that are particularly helpful are 0x001 (Informational messages) and 0x002 (Error messages)

So to use this, for example, you could type fsck_hfs -D 0x001

To use multiple flags, including -D, you could type fsck_hfs -dflE -D 0x001


notes

You can specify (after the command and flags) what filesystem you want to check. If you do not specify, all filesystems will be checked, which is most likely the desire of the user.

Listing disks and volumes in OSX will not be covered in this article during any revisions, and will be in a separate article that will be linked to here in the future. This article will not touch upon specifying filesystems for this reason.