{"id":12697,"date":"2019-03-28T13:46:17","date_gmt":"2019-03-28T13:46:17","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12697"},"modified":"2019-03-28T13:46:17","modified_gmt":"2019-03-28T13:46:17","slug":"how-to-repair-and-defragment-linux-system-partitions-and-directories","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/how-to-repair-and-defragment-linux-system-partitions-and-directories\/","title":{"rendered":"How to Repair and Defragment Linux System Partitions and Directories"},"content":{"rendered":"<p>People who use Linux often think that it doesn\u2019t require defragmentation. This is a common misunderstanding across Linux users. Actually, the Linux operating system does support defragmentation. The point of the defragmentation is to improve I\/O operations like allowing local videos to load faster or extracting archives significantly faster.<\/p>\n<div id=\"attachment_17308\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Defragment-Linux-System-Partitions.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17308\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Defragment-Linux-System-Partitions.png\" alt=\"Defragment Linux System Partitions\" width=\"717\" height=\"341\" aria-describedby=\"caption-attachment-17308\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17308\" class=\"wp-caption-text\">Defragment Linux System Partitions and Directories<\/p>\n<\/div>\n<p>The Linux ext2, ext3 and ext4 filesystems don\u2019t need that much attention, but with time, after executing many many many read\/writes the filesystem may require optimization. Otherwise the hard disk might become slower and may affect the entire system.<\/p>\n<p>In this tutorial I am going to show you few different techniques to perform defragmentation on files. Before we start, we should mention\u00a0what the common filesystems like ext2,3,4 do to prevent fragmentation. These filesystems include technique\u00a0to prevent\u00a0the effect.\u00a0For example filesystems reserve free block groups on the hard disk to store growing files completely.<\/p>\n<p>Unfortunately the problem is not always solved with such mechanism. While other operating systems may require expensive additional software to resolve such issues, Linux has some easy to install\u00a0tools that can help you resolve such problems.<\/p>\n<h3>How to Check a Filesystem Requires Defragmentation?<\/h3>\n<p>Before we start I would like to point that the operations below should only be ran on\u00a0<strong>HDDs<\/strong>\u00a0and not on\u00a0<strong>SSD<\/strong>.\u00a0Defragging your SSD drive will only increase its read\/write count and therefore shorten it\u2019s life. Instead, if you are using SSD, you should use the TRIM function, which is not covered in this tutorial.<\/p>\n<p>let\u2019s test if the system actually requires defragmentation. We can easily check this with tool such as\u00a0<strong>e2fsck<\/strong>. Before you use this tool on a partition on your system, it is recommended\u00a0to\u00a0unmount that partition with. This is not completely necessary, but it\u2019s the safe way to go:<\/p>\n<pre>$ sudo umount &lt;device file&gt;\r\n<\/pre>\n<p>In my case I have\u00a0<strong>\/dev\/sda1<\/strong>\u00a0mounted at\u00a0<strong>\/tmp<\/strong>:<\/p>\n<div id=\"attachment_17299\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/disk-partition-before.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17299\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/disk-partition-before.png\" alt=\"Disk Partition Table Before\" width=\"956\" height=\"472\" aria-describedby=\"caption-attachment-17299\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17299\" class=\"wp-caption-text\">Disk Partition Table Before<\/p>\n<\/div>\n<p>Keep in mind that\u00a0in your case the partition table might be different so make sure to\u00a0<strong>unmount<\/strong>\u00a0the right partition. To unmount that partition you can use:<\/p>\n<pre>$ sudo umount \/dev\/sda1\r\n<\/pre>\n<p>Now let\u2019s check if this partition requires defragmentation, with\u00a0<strong>e2fsck<\/strong>. You will need to run the following command:<\/p>\n<pre>$ sudo e2fsck -fn \/dev\/sda1\r\n<\/pre>\n<p>The above command will perform a file system check. The\u00a0<strong>-f<\/strong>\u00a0option forces the check, even if the system seems clean. The\u00a0<strong>-n<\/strong>\u00a0option is used to open the filesystem in read-only and assume answer of\u00a0<code>\"no\"<\/code>\u00a0to all questions that may appear.<\/p>\n<p>This options basically allows to use\u00a0<strong>e2fsck<\/strong>\u00a0non-interactively.\u00a0If everything is\u00a0<strong>Okay<\/strong>, you should see result similar to the one shown on the screenshot below:<\/p>\n<div id=\"attachment_17300\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/e2fsck-healthy-partition.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17300\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/e2fsck-healthy-partition.png\" alt=\"e2fsck Healthy Partition\" width=\"1024\" height=\"278\" aria-describedby=\"caption-attachment-17300\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17300\" class=\"wp-caption-text\">e2fsck Healthy Partition<\/p>\n<\/div>\n<p>Here is another example that shows errors on a system:<\/p>\n<div id=\"attachment_17301\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/e2fsck-with-errors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17301\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/e2fsck-with-errors.png\" alt=\"e2fsck With Errors\" width=\"1310\" height=\"940\" aria-describedby=\"caption-attachment-17301\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17301\" class=\"wp-caption-text\">e2fsck With Errors<\/p>\n<\/div>\n<h3>How to Repair Linux Filesystem Using e2fsck<\/h3>\n<p>If errors appear, you can attempt a repair of the filesystem with\u00a0<strong>e2fsck<\/strong>\u00a0with the \u201c<strong>-p<\/strong>\u201d option. Note that in order to run the command below, the partition will need to be unmounted:<\/p>\n<pre>$ sudo e2fsck -p &lt;device file&gt;\r\n<\/pre>\n<p>The \u201c<strong>-p<\/strong>\u201d options attempts automatic repair on the file system for problems that can be safely fixed without human intervention. If a problem is discovered that may require the system administrator to take additional corrective action, e2fsck will print\u00a0a description of the problem and will exit with code 4, which means \u201c<b>File system errors left uncorrected\u201d. Depending on the issue that has been found, different actions might be required.<\/b><\/p>\n<p>If the issue appears on a partition that cannot be unmounted, you can use another tool called\u00a0<strong>e4defrag<\/strong>. It comes pre-installed on many Linux distros, but if you don\u2019t have it on yours, you can install it with:<\/p>\n<pre>$ sudo apt-get install\u00a0e2fsprogs         [On Debian and Derivatives]\r\n# yum install e2fsprogs                  [On CentOS based systems]\r\n# dnf install e2fsprogs                  [On Fedora 22+ versions] \r\n<\/pre>\n<h3>How to Defragment Linux Partitions<\/h3>\n<p>Now it\u2019s time to defragment Linux partitions using following command.<\/p>\n<pre>$ sudo e4defrag\u00a0&lt;location&gt;\r\nor\r\n$ sudo e4defrag &lt;device&gt;\r\n<\/pre>\n<h3>How to Defragment Linux Directory<\/h3>\n<p>For example, if you wish to defragment a single directory or device, you can use:<\/p>\n<pre>$ sudo e4defrag \/home\/user\/directory\/\r\n# sudo e4defrag \/dev\/sda5\r\n<\/pre>\n<h3>How to Defragment All Linux Partitions<\/h3>\n<p>If you prefer to defragment your entire system, the safe way of doing this is:<\/p>\n<pre>$ sudo e4defrag \/\r\n<\/pre>\n<p>Keep in mind that this process may take some time to be completed.<\/p>\n<h3>Conclusion<\/h3>\n<p><strong>Defragmentation<\/strong>\u00a0is an operation that you will rarely need to run in Linux. It\u2019s meant for power users who know what exactly they are doing and is not recommended for Linux newbies. The point of the whole action is to have your filesystem optimized so that new read\/write operations are performed more efficiently.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/defragment-linux-system-partitions-and-directories\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>People who use Linux often think that it doesn\u2019t require defragmentation. This is a common misunderstanding across Linux users. Actually, the Linux operating system does support defragmentation. The point of the defragmentation is to improve I\/O operations like allowing local videos to load faster or extracting archives significantly faster. Defragment Linux System Partitions and Directories &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/how-to-repair-and-defragment-linux-system-partitions-and-directories\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Repair and Defragment Linux System Partitions and Directories&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12697","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12697"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12697\/revisions"}],"predecessor-version":[{"id":12698,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12697\/revisions\/12698"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}