10 Linux Fsck Command Examples to Check and Repair Filesystem

by Balakrishnan Mariyappan on August 22, 2012

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

First, view all the available partitions on your system using parted command as shown below.

# 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

You can check a specific filesystem (for example: /dev/sda6) as shown below.

# 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

The following are the possible exit codes for fsck command.

  • 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.

# fsck -A

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.

# fsck -M /dev/sda7
# echo $?
0

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.

# fsck -a -AR

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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 12 comments… read them below or add one }

1 Jalal Hajigholamali August 22, 2012 at 11:45 am

HI,

Thanks, nice and useful article…

2 Ashok August 22, 2012 at 9:02 pm

Too Good article on ‘fsck’. I don’t think anyone can explain more better than this :)

3 Arivuchelvan August 23, 2012 at 12:04 am

Hi,
Nice tips. Write more.. Thanks..

4 Ethan August 23, 2012 at 12:50 am

Good instructions. The parted commond is not installed in Debian Linux, by default. I apt-get installed it and tried some of the examples in my netbook.Though only a mount of simpler result produced,

5 Karthigayan August 23, 2012 at 11:51 pm

Useful article.Keep writing.Thanks.

6 bohabork August 31, 2012 at 8:36 am

#su
# cd /
# touch forcefsck
# shutdown -r now

…or …

# cd /
# sudo touch forcefsck
# sudo shutdown -r now

…zen sitteren quietly und watchen ze blinking lichts ;-)

7 Frank March 22, 2013 at 11:50 am

What options would be needed for either e2fsck, fsck to check, and if repair is impossible add to a badblocks list. To prevent future use. Assuming a LiveCD is used to check the hard disks. including a swap drive (single partition that fills the 20gb)

8 soroush April 17, 2013 at 10:39 pm

hi
soroush@soroush:~$ sudo fsck /dev/sda1
fsck from util-linux 2.20.1
e2fsck 1.42.5 (29-Jul-2012)
The filesystem size (according to the superblock) is 24414062 blocks
The physical size of the device is 24413952 blocks
Either the superblock or the partition table is likely to be corrupt!

i confused :|

9 ROmanul June 26, 2013 at 4:50 pm

I have a dedicated server.
Every fsck badblocks or other command said:

/dev/sda is in use.
e2fsck: Cannot continue, aborting.

An operation so simple in Windows is so complicated in linux.
Server is thousands of miles away from me.

10 Frank June 28, 2013 at 4:14 am

Hi ROmanul,
/dev/sda is normally where the /root system is on Linux systems.
And you cannot check it while it’s online,
Similar to MS Windows which you can only really check for and fix physical damage while off-line. Depending on the Distro you have, I would use their mailing-list for specific questions, detailing how you connect to the remote box. If you have s support contract just contact them.

11 falkor March 2, 2014 at 5:05 pm

i recently lost my OS,
it contained a 120gb SSD (for the os) and two separate 750GB (matching build) HD’s.

when i try to mount and copy over the old 750gb hds, they fail,
they wont mount up, and when i try to fsck i get this error.

[root@omited]# fsck -y /dev/sdd
fsck from util-linux 2.19.1
e2fsck 1.41.14 (22-Dec-2010)
fsck.ext2: Superblock invalid, trying backup blocks…
fsck.ext2: Bad magic number in super-block while trying to open /dev/sdd

The superblock could not be read or does not describe a correct ext2
filesystem. If the device is valid and it really contains an ext2
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck -b 8193

any help is appreciated.

12 Jhon Edison April 12, 2014 at 11:11 am

Gracias, muy util!!!

Leave a Comment

Previous post:

Next post: