{"id":4821,"date":"2014-05-04T13:02:24","date_gmt":"2014-05-04T13:02:24","guid":{"rendered":"http:\/\/www.webputzer.com\/?p=4821"},"modified":"2018-09-06T20:59:21","modified_gmt":"2018-09-06T20:59:21","slug":"4821","status":"publish","type":"post","link":"http:\/\/www.webputzer.com\/?p=4821","title":{"rendered":"Using fsck to check and repair Linux filesystems"},"content":{"rendered":"<p>No matter how well you take care of your computer, sometimes, it will just crash and you often have to pull off the plug to restart the computer. When such crashes happen, there is a chance that your filesystem will get corrupted or damage. It is advisable to do a filesystem check regularly to make sure that it is running properly and free of error.<\/p>\n<p>In Linux (and Mac), there is this powerful command \u201c<code>fsck<\/code>\u201d that you can use to check and repair your filesystem. \u201cFsck\u201d stands for \u201cFile System Consistency checK\u201d.<\/p>\n<p>The usage is very easy. Open a terminal and type:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>fsck \/dev\/sda1<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>This will check the sda1 partition. An important thing to note is that fsck cannot be used on a mounted partition. If you do so, there is a high chance that it will damage the filesystem. To check your Home folder that resides on another partition, say sda2, use the following commands:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>umount \/home\r\nfsck \/dev\/sda2<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><strong>Note<\/strong>: you will need root\/superuser permission to run the \u201cfsck\u201d command.<\/p>\n<p>You can also use \u201cfsck\u201d to check\u00a0<a title=\"Link added by VigLink\" href=\"http:\/\/viglink.pgpartner.com\/mrdr.php?url=http%3A%2F%2Fviglink.pgpartner.com%2Fsearch.php%2Fform_keyword%3Dexternal%2Bdrive&amp;mode=\" rel=\"nofollow\">external\u00a0drive<\/a>, such as your thumb drive or SD card. For example:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>umount \/dev\/sdb1 #thumb drive\r\nsudo fsck \/dev\/sdb1<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>If you are not sure of the partition number, you can use the command<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>sudo fdisk -l<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>to list out all the partitions in the system.<\/p>\n<h2>Advanced Usage<\/h2>\n<p>There are a few parameters that you can add to \u201cfsck\u201d to make it more powerful.<\/p>\n<h3>Auto repair filesystem when errors are detected<\/h3>\n<p>During the filesystem check, if errors are detected, you can get \u201cfsck\u201d to auto repair the filesystem with the\u00a0<code>-a<\/code>\u00a0flag. For example:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>fsck -a \/dev\/sda1<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Similarly, using the\u00a0<code>-y<\/code>\u00a0flag can get the job done as well:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>fsck -y \/dev\/sda1<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>Check all filesystems in one run<\/h3>\n<p>If there are several filesystems in your computer, you can get fsck to check all of them at the same time with the\u00a0<code>-A<\/code>\u00a0flag.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>fsck -A<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>What it will do is to grab all the filesystem entries from \/etc\/fstab and scan them for errors. You can use it together with the\u00a0<code>-R<\/code>\u00a0and\u00a0<code>-y<\/code>\u00a0flag to prevent it from scanning the root filesystem and fix all errors, if there is any.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>fsck -AR -y<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>Exclude check on mounted filesystem<\/h3>\n<p>As mentioned earlier, fsck cannot be run on a mounted filesystem. If you are using the\u00a0<code>-A<\/code>\u00a0flag to scan all the filesystems, and some of them are mounted, you might damage those filesystems. A way to overcome this is to use the\u00a0<code>-M<\/code>\u00a0flag to prevent it from checking mounted system.<\/p>\n<p>For example, running the command<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>fsck -M \/dev\/sdc1<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>returns nothing and a return code 0 (which means \u201cno error\u201d). No scan was done at all since all the filesystems are mounted.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"fsck-exclude-mounted-filesystem\" src=\"http:\/\/mcdn.maketecheasier.com\/wp-content\/uploads\/2013\/09\/fsck-exclude-mounted-filesystem.png\" width=\"1\" height=\"1\" \/><\/p>\n<p>However, if I unmount the external\u00a0<a title=\"Link added by VigLink\" href=\"http:\/\/viglink.pgpartner.com\/mrdr.php?url=http%3A%2F%2Fviglink.pgpartner.com%2Fsearch.php%2Fform_keyword%3Dthumb%2Bdrive&amp;mode=\" rel=\"nofollow\">thumb\u00a0drive<\/a>\u00a0and run the command again, it will then perform the scan and return the output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"fsck-check-unmounted-external-filesystem\" src=\"http:\/\/mcdn.maketecheasier.com\/wp-content\/uploads\/2013\/09\/fsck-check-unmounted-external-filesystem.png\" width=\"1\" height=\"1\" \/><\/p>\n<h3>Specifying the filesystem type<\/h3>\n<p>There are times when you just want to check filesystems of a specific type, say ext2. You can make use of the\u00a0<code>-t<\/code>\u00a0flag to specify the filesystem type to check. For example, the command<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>fsck -t ext4 \/dev\/sdc1<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>will scan the\u00a0<a title=\"Link added by VigLink\" href=\"http:\/\/viglink.pgpartner.com\/mrdr.php?url=http%3A%2F%2Fviglink.pgpartner.com%2Fsearch.php%2Fform_keyword%3Dexternal%2Bdrive&amp;mode=\" rel=\"nofollow\">external\u00a0drive<\/a>\u00a0only if it is in the ext4 format. Alternatively, you can combine with the\u00a0<code>-A<\/code>flag to scan all filesystems of the specific type:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>fsck -A -t ext4 -y<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>Force fsck to run everytime during bootup<\/h2>\n<p>By default, Ubuntu will run fsck after every 30 bootups, but if you want the system to to do a \u201cfsck\u201d check everytime it boots up, all you need to do is to create an empty file call \u201cforcefsck\u201d and place it in the root folder. This will notify the system to do a \u201cfsck\u201d check everytime during\u00a0<a title=\"Link added by VigLink\" href=\"http:\/\/viglink.pgpartner.com\/rd.php?r=5316&amp;m=1422485729&amp;q=n&amp;rdgt=1399047250&amp;it=1399220050&amp;et=1399652050&amp;priceret=13.52&amp;pg=~~3&amp;k=51d9f17073be72cce95ac1e29b36d2c8&amp;source=feed&amp;url=http%3A%2F%2Fwww%2Eamazon%2Ecom%2Fdp%2FB005RCHTPK%2Fref%3Dasc%5Fdf%5FB005RCHTPK3088776%3Fsmid%3DA21C4U5X700J66%26tag%3Dpgmp%2D473%2D01%2D20%26linkCode%3Ddf0%26creative%3D395109%26creativeASIN%3DB005RCHTPK&amp;st=feed&amp;mt=~~~~~~~~n~~~\" rel=\"nofollow\">boot\u00a0up<\/a>.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>sudo touch \/forcefsck<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>To change the checking frequency, you can make use of the command \u201c<code>tune2fs<\/code>\u201c.<\/p>\n<p>The following command instructs the system to run \u201cfsck\u201d after every 30\u00a0<a title=\"Link added by VigLink\" href=\"http:\/\/viglink.pgpartner.com\/rd.php?r=5316&amp;m=1422485729&amp;q=n&amp;rdgt=1399047250&amp;it=1399220050&amp;et=1399652050&amp;priceret=13.52&amp;pg=~~3&amp;k=51d9f17073be72cce95ac1e29b36d2c8&amp;source=feed&amp;url=http%3A%2F%2Fwww%2Eamazon%2Ecom%2Fdp%2FB005RCHTPK%2Fref%3Dasc%5Fdf%5FB005RCHTPK3088776%3Fsmid%3DA21C4U5X700J66%26tag%3Dpgmp%2D473%2D01%2D20%26linkCode%3Ddf0%26creative%3D395109%26creativeASIN%3DB005RCHTPK&amp;st=feed&amp;mt=~~~~~~~~n~~~\" rel=\"nofollow\">boot\u00a0up<\/a>.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>tune2fs -c 30 \/dev\/sdaX<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>You can also specify number of days instead of\u00a0<a title=\"Link added by VigLink\" href=\"http:\/\/viglink.pgpartner.com\/rd.php?r=5316&amp;m=1422485729&amp;q=n&amp;rdgt=1399047250&amp;it=1399220050&amp;et=1399652050&amp;priceret=13.52&amp;pg=~~3&amp;k=51d9f17073be72cce95ac1e29b36d2c8&amp;source=feed&amp;url=http%3A%2F%2Fwww%2Eamazon%2Ecom%2Fdp%2FB005RCHTPK%2Fref%3Dasc%5Fdf%5FB005RCHTPK3088776%3Fsmid%3DA21C4U5X700J66%26tag%3Dpgmp%2D473%2D01%2D20%26linkCode%3Ddf0%26creative%3D395109%26creativeASIN%3DB005RCHTPK&amp;st=feed&amp;mt=~~~~~~~~n~~~\" rel=\"nofollow\">boot\u00a0up<\/a>:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>sudo tune2fs -i 10d \/dev\/sdaX<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>Using fsck with a GUI<\/h2>\n<p>As you can see, fsck is mainly a command line tool. If you need a GUI for that, GParted comes with a feature that allows you to check your filesystem.<\/p>\n<p>First, install GParted:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>sudo apt-get install gparted<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Next, open GParted and select the filesystem you want to check. Right click on it and select \u201cCheck\u201d.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"fsck-gparted-check-filesystem\" src=\"http:\/\/mcdn.maketecheasier.com\/wp-content\/uploads\/2013\/09\/fsck-gparted-check-filesystem.png\" width=\"1\" height=\"1\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"fsck-gparted-filesystem-checked\" src=\"http:\/\/mcdn.maketecheasier.com\/wp-content\/uploads\/2013\/09\/fsck-gparted-filesystem-checked.png\" width=\"1\" height=\"1\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>fsck is a useful command that can check your filesystem for errors and fix them automatically. Most of the time, you won\u2019t have to bother with it as your system will usually run it regularly during\u00a0<a title=\"Link added by VigLink\" href=\"http:\/\/viglink.pgpartner.com\/rd.php?r=5316&amp;m=1422485729&amp;q=n&amp;rdgt=1399047250&amp;it=1399220050&amp;et=1399652050&amp;priceret=13.52&amp;pg=~~3&amp;k=51d9f17073be72cce95ac1e29b36d2c8&amp;source=feed&amp;url=http%3A%2F%2Fwww%2Eamazon%2Ecom%2Fdp%2FB005RCHTPK%2Fref%3Dasc%5Fdf%5FB005RCHTPK3088776%3Fsmid%3DA21C4U5X700J66%26tag%3Dpgmp%2D473%2D01%2D20%26linkCode%3Ddf0%26creative%3D395109%26creativeASIN%3DB005RCHTPK&amp;st=feed&amp;mt=~~~~~~~~n~~~\" rel=\"nofollow\">boot\u00a0up<\/a>to make sure everything is working fine. However, when your filesystem get corrupted, this is the easiest way to find out what happen and fix it.<\/p>\n<p>Note: fsck is also available in Mac.<\/p>\n<h2>Regarding Logging<\/h2>\n<p>Your original problem can be solved with output redirection, a standard<br \/>\npractice in Unix\/Linux. To do that, you will need to force fsck in non<br \/>\ninteractive mode (the default is interactive mode, where you will need a<br \/>\nterminal). To force fsck to operate in non interactive mode, you can use<br \/>\nthe -p flag (see manual page)<\/p>\n<p>If you use the -p flag like this:<\/p>\n<pre>fsck -v -p \/machine\/disk\/p1 2>&1 > fscklog.txt<\/pre>\n<p>the log file fscklog.txt will contain the output of fsck on partition<br \/>\n\/machine\/disk\/u1.<\/p>\n<p>(I assume that you are OK asking the utility to non interactively<br \/>\ncorrect all the FS errors it finds in batch mode. If you wish to dump a<br \/>\nlog of the initial stages of fsck command without fixing the errors use<br \/>\na combonation of -p and -n flags (see manual page)).<\/p>\n<p>Either way, you will have a log of the fsck output wherever you wish<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No matter how well you take care of your computer, sometimes, it will just crash and you often have to pull off the plug to restart the computer. When such crashes happen, there is a chance that your filesystem will get corrupted or damage. It is advisable to do a filesystem check regularly to make [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4177,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"h5ap_radio_sources":[],"footnotes":""},"categories":[21],"tags":[],"class_list":["post-4821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"http:\/\/www.webputzer.com\/index.php?rest_route=\/wp\/v2\/posts\/4821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.webputzer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.webputzer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.webputzer.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.webputzer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4821"}],"version-history":[{"count":0,"href":"http:\/\/www.webputzer.com\/index.php?rest_route=\/wp\/v2\/posts\/4821\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.webputzer.com\/index.php?rest_route=\/wp\/v2\/media\/4177"}],"wp:attachment":[{"href":"http:\/\/www.webputzer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.webputzer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4821"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.webputzer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}