- 10 Linux Fsck Command Examples to Check and Repair Filesystem
- 1. Filesystem Check on a Disk Partition
- 2. Fsck Command Specific to a Filesystem Type
- 3. Check All Filesystems in One Run using Option -A
- 4. Check Only a Specific Filesystem Type using Option -t
- 5. Don’t execute Fsck on Mounted Filesystem using Option -M
- 6. Skip the Display Title using Option -T
- 7. Force a Filesystem Check Even if it’s Clean using Option -f
- 8. Attempt to Fix Detected Problems Automatically using Option -y
- 9. Avoid Repair, but Report Problems to Stdout using Option -n
- 10. Automatically Repair the Damaged Portions using Option -a
10 Linux Fsck Command Examples to Check and Repair Filesystem
Linux fsck utility is used to check and repair Linux filesystems (ext2, ext3, ext4, etc.). Depending on when was the last time a file system was checked, the system runs the fsck during boot time to check whether the filesystem is in consistent state. System administrator could also run it manually when there is a problem with the filesystems. Make sure to execute the fsck on an unmounted file systems to avoid any data corruption issues.
This article explains 10 practical examples on how to execute fsck command to troubleshoot and fix any filesystem errors.
1. Filesystem Check on a Disk Partition
# parted /dev/sda 'print' Number Start End Size Type File system Flags 1 1049kB 106MB 105MB primary fat16 diag 2 106MB 15.8GB 15.7GB primary ntfs boot 3 15.8GB 266GB 251GB primary ntfs 4 266GB 500GB 234GB extended 5 266GB 466GB 200GB logical ext4 6 467GB 486GB 18.3GB logical ext2 7 487GB 499GB 12.0GB logical fat32 lba
# fsck /dev/sda6 fsck from util-linux 2.20.1 e2fsck 1.42 (29-Nov-2011) /dev/sda6: clean, 95/2240224 files, 3793506/4476416 blocks
- 0 – No errors
- 1 – Filesystem errors corrected
- 2 – System should be rebooted
- 4 – Filesystem errors left uncorrected
- 8 – Operational error
- 16 – Usage or syntax error
- 32 – Fsck canceled by user request
- 128 – Shared-library error
2. Fsck Command Specific to a Filesystem Type
fsck internally uses the respective filesystem checker command for a filesystem check operation. These fsck checker commands are typically located under /sbin.
The following example show the various possible fsck checker commands (for example: fsck.ext2, fsck.ext3, fsck.ext4, etc.)
# cd /sbin # ls fsck* fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.ext4 fsck.ext4dev fsck.minix fsck.msdos fsck.nfs fsck.vfat
fsck command will give you an error when it doesn’t find a filesystem checker for the filesystem that is being checked.
For example, if you execute fsck over a ntfs partition, you’ll get the following error message. There is no fsck.ntfs under /sbin. So, this gives the following error message.
# fsck /dev/sda2 fsck from util-linux 2.20.1 fsck: fsck.ntfs: not found fsck: error 2 while executing fsck.ntfs for /dev/sda2
3. Check All Filesystems in One Run using Option -A
You can check all the filesystems in a single run of fsck using this option. This checks the file system in the order given by the fs_passno mentioned for each filesystem in /etc/fstab.
Please note that the filesystem with a fs_passno value of 0 are skipped, and greater than 0 are checked in the order.
The /etc/fstab contains the entries as listed below,
# cat /etc/fstab ## proc /proc proc nodev,noexec,nosuid 0 0 ## / was on /dev/sda5 during installation /dev/sda5 / ext4 errors=remount-ro 0 1 ## /mydata was on /dev/sda6 during installation /dev/sda6 /mydata ext2 defaults 0 2 ## /backup was on /dev/sda7 during installation /dev/sda7 /backup vfat defaults 0 3
Here, the filesystem with the same fs_passno are checked in parallel in your system.
It is recommended that you exclude the root filesystem during this global check by adding -R option as shown below.
# fsck -AR -y fsck from util-linux 2.20.1 e2fsck 1.42 (29-Nov-2011) /dev/sda6: clean, 95/2240224 files, 3793506/4476416 blocks dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN /dev/sda7: 8 files, 50/1463400 clusters
Note: Option -y is explained in one of the examples below.
4. Check Only a Specific Filesystem Type using Option -t
Using fsck -t option, you can specify the list of filesystem to be checked. When you are using with option -A, the fsck will check only the filesystem mentioned with this option -t. Note that fslist is a comma separated values.
Now, pass ext2 as the fslist value to -t option as shown below:
# fsck -AR -t ext2 -y fsck from util-linux 2.20.1 e2fsck 1.42 (29-Nov-2011) /dev/sda6: clean, 11/2240224 files, 70327/4476416 blocks
In this example, /dev/sda6 is the only partition created with the filesystem ext2, thus it get checked accordingly.
Using the keyword ‘no’ in front of filesystem, you can check all other filesystem types except a particular filesystem.
In the following example, ext2 filesystem is excluded from the check.
# fsck -AR -t noext2 -y fsck from util-linux 2.20.1 dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN /dev/sda7: 0 files, 1/1463400 clusters
5. Don’t execute Fsck on Mounted Filesystem using Option -M
It is a good idea to use this option as default with all your fsck operation. This prevents you from running fsck accidentally on a filesystem that is mounted.
# mount | grep "/dev/sd*" /dev/sda5 on / type ext4 (rw,errors=remount-ro) /dev/sda6 on /mydata type ext2 (rw) /dev/sda7 on /backup type vfat (rw)
As shown above, /dev/sda7 is mounted. If you try to execute fsck on this /dev/sda7 mounted filesystem (along with the -M option), fsck will simply exit with the exit code 0 as shown below.
6. Skip the Display Title using Option -T
Using option -T, you can skip the title get displayed in the beginning of fsck command output.
# fsck -TAR e2fsck 1.42 (29-Nov-2011) /dev/sda6 is mounted. e2fsck: Cannot continue, aborting. dosfsck 3.0.12, 29 Oct 2011, FAT32, LFN /dev/sda7: 8 files, 50/1463400 clusters
Note that the title is something like “fsck from util-linux 2.20.1”.
7. Force a Filesystem Check Even if it’s Clean using Option -f
By default fsck tries to skip the clean file system to do a quicker job.
# fsck /dev/sda6 fsck from util-linux 2.20.1 e2fsck 1.42 (29-Nov-2011) /dev/sda6: clean, 95/2240224 files, 3793503/4476416 blocks
You can force it to check the file system using -f as shown below.
# fsck /dev/sda6 -f fsck from util-linux 2.20.1 e2fsck 1.42 (29-Nov-2011) 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/sda6: 95/2240224 files (7.4% non-contiguous), 3793503/4476416 blocks
8. Attempt to Fix Detected Problems Automatically using Option -y
In the following example, /dev/sda6 partition is corrupted as shown below.
# mount /dev/sda6 /mydata # cd /mydata # ls -li ls: cannot access test: Input/output error total 72 49061 -rw-r--r-- 1 root root 8 Aug 21 21:50 1 49058 -rw-r--r-- 1 root root 36864 Aug 21 21:24 file_with_holes 49057 -rw-r--r-- 1 root root 8192 Aug 21 21:23 fwh 11 drwxr-xr-x 2 root root 49152 Aug 19 00:29 lost+found 2060353 ?rwSr-S-wT 16 root root 4096 Aug 21 21:11 Movies ? -. ? ? ? ? ? test
As seen above, the directory Movies and a file test attributes are invalid.
In the following example, -y will pass “yes” to all the questions to fix the detected corruption automatically.
# fsck -y /dev/sda6 fsck from util-linux 2.20.1 e2fsck 1.42 (29-Nov-2011) /dev/sda6 contains a file system with errors, check forced. Pass 1: Checking inodes, blocks, and sizes Inode 2060353 is a unknown file type with mode 0137642 but it looks like it is really a directory. Fix? yes Pass 2: Checking directory structure Entry 'test' in / (2) has deleted/unused inode 49059. Clear? yes Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/sda6: ***** FILE SYSTEM WAS MODIFIED ***** /dev/sda6: 96/2240224 files (7.3% non-contiguous), 3793508/4476416 blocks
9. Avoid Repair, but Report Problems to Stdout using Option -n
It is possible to print such detected problems into stdout without repairing the filesystem using fsck -n option.
First, you could notice/see the problem in partition /dev/sda6 that the Movies directory (and fwh file) doesn’t have valid attribute details.
# mount /dev/sda6 /mydata # cd /mydata # ls -lrt total 64 drwxr-xr-x 2 root root 49152 Aug 19 00:29 lost+found ?--xrwx-wx 16 root root 4096 Aug 21 21:11 Movies ?-----x-wx 1 root root 8192 Aug 21 21:23 fwh -rw-r--r-- 1 root root 36864 Aug 21 21:24 file_with_holes -rw-r--r-- 1 root root 8 Aug 21 21:50 1
The above problem in the specific partition displayed in stdout without doing any fix on it as follows,
The following fsck example displays the problem in the stdout without fixing it. (partial output is shown below).
# fsck -n /dev/sda6 fsck from util-linux 2.20.1 e2fsck 1.42 (29-Nov-2011) /dev/sda6 contains a file system with errors, check forced. Pass 1: Checking inodes, blocks, and sizes Inode 2060353 is a unknown file type with mode 0173 but it looks like it is really a directory. Fix? no Inode 2060353, i_blocks is 8, should be 0. Fix? no Pass 2: Checking directory structure Inode 2060353 (/Movies) has invalid mode (0173). Clear? no Inode 49057 (/fwh) has invalid mode (013). Clear? no Entry 'fwh' in / (2) has an incorrect filetype (was 1, should be 0). Fix? no Pass 3: Checking directory connectivity Unconnected directory inode 65409 (. ) Connect to /lost+found? no '..' in . (65409) is . (2060353), should be (0). Fix? no Unconnected directory inode 2076736 (. ) Connect to /lost+found? no Pass 4: Checking reference counts Inode 2 ref count is 4, should be 3. Fix? no Inode 65409 ref count is 3, should be 2. Fix? no Inode 2060353 ref count is 16, should be 15. Fix? no Unattached inode 2060354 Connect to /lost+found? no Pass 5: Checking group summary information Block bitmap differences: -(164356--164357) -4149248 Fix? no Directories count wrong for group #126 (1, counted=0). Fix? no /dev/sda6: ********** WARNING: Filesystem still has errors ********** /dev/sda6: 96/2240224 files (7.3% non-contiguous), 3793508/4476416 blocks
10. Automatically Repair the Damaged Portions using Option -a
In order to repair the damaged portion automatically ( without any user interaction ), use the option -a as shown below.
The option -a is same as -p in e2fsck utility. It cause e2fsck to fix any detected problems that has to be safely fixed without user interaction.
In case when fsck requires administrator’s attention, it simply exits with error code 4 before printing the description of the problem.
# fsck -a /dev/sda6 fsck from util-linux 2.20.1 /dev/sda6 contains a file system with errors, check forced. /dev/sda6: Inode 2060353 is a unknown file type with mode 0173 but it looks like it is really a directory. /dev/sda6: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY. (i.e., without -a or -p options) # echo $? 4
As you remember, fsck -y option can be used here to fix the above issue automatically.
# fsck -y /dev/sda6 fsck from util-linux 2.20.1 e2fsck 1.42 (29-Nov-2011) /dev/sda6 contains a file system with errors, check forced. Pass 1: Checking inodes, blocks, and sizes Inode 2060353 is a unknown file type with mode 0173 but it looks like it is really a directory. Fix? yes Pass 2: Checking directory structure Inode 49057 (/fwh) has invalid mode (013). Clear? yes Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information Block bitmap differences: -(164356--164357) Fix? yes Free blocks count wrong for group #5 (0, counted=2). Fix? yes Free blocks count wrong (682908, counted=682910). Fix? yes /dev/sda6: ***** FILE SYSTEM WAS MODIFIED ***** /dev/sda6: 95/2240224 files (7.4% non-contiguous), 3793506/4476416 blocks