Well, most of the times you would hear from a lot sources that hard disks in Linux systems does not need any defragmentation at all and though this might be true, but to certain limitations for sure. There are some specific instances in which fragmentation does develop in the hard drive of your system under the Linux OS despite its using one of the best journaling file systems. No worries! In case you notice a noticeable amount of fragmentation developing in your HDD in Linux, here are some easy ways to defragment hard disk in Linux.
Hard Drive Defragmentation Procedure
Ways to Defragment Hard Disk in Linux
Although, defragmentation is not really needed in Linux systems but in case you do need to tune up your hard drive if it is small or running out of space. Some possible causes of fragmentation can be:
- Having small capacity hard disks or older hardware.
- Having tiny partitions that clutters your HD a lot.
- Editing or working on large raw files/videos.
Apart from doing a defragmentation of your hard disk, if you have small hard disk and face fragmentation or space related problems continuously, it is much better to actually buy a new hard drive of more capacity. However, if this cannot be done in due time and you need to carry on important procedures, you should better proceed with defragmentation of your HD. Here’s how you can check for the fragmentation level in your hard disk in Linux.
In Linux the “fsck” command checks for all the fragmentation level on hard disk in Linux. However, this will require you to have your original Linux installation disk from which you will boot on all un-mounted partitions.
Important: Please be aware that processing “fsck” command on mounted partitions can result in a severe damage to your Linux system and hard drive so always ensure to create backup before proceeding with this command.
Now, after booting from your Linux installation or rescue disk run “fsck” command on the un-mounted partition. In order to make sure there are no problems with the partition, run this command with root permits:
“fsck –fn [/path/to/your/partition]”
To check the path of your partition, run this command:
“sudo fdisk -1”
Also, there is a much safer means to execute “fsck” command on a mounted partition. You need to add the “-n” switch only after typing “fsck” since this will only read the file system and check without interfering with anything else. But again, to defragment hard disk in Linux safely, it is advised to create backup prior to any kind of such actions in case things go on the wrong course. Now, after backing up you should run the following command on ext2 file system:
“sudo fsck.ext2 –fn /path/to/your/partition”
This will reveal to you the amount of fragmentation on your drive partition after showing some errors which are of no importance in the end. So, if the fragmentation level increases above 20% then it means you need to defragment your hard disk.
Again, before proceeding with fragmentation just do a back up to some external storage source apart from your hard drive buy copy pasting all data. Now, format your partition and paste the files back to the partition. There are some commands for each step you need to use from now on to properly defragment hard disk in Linux. First, to start copy pasting process in Linux, use this command:
“cp –afv [/path/to/source/partition]/* [/path/to/destination/folder]”
After copy pasting, now you should use this command to delete all the original files:
“sudo rm –rf [/path/to/source/partition/]/*”
As an optional operation, you can fill up the empty spaces with zeros. This can be obtained through formatting as well but in case you do not copying whole partition but only huge files, as these will create chunks and fragments, this would not be a suitable option for you then.
Now, proceed with this command to defragment hard disk in Linux:
“sudo apt-get install e2fsprongs”
Now run e4defrag in root mode on the fragmented partition. In case you cannot mount or un-mount a partition, you can utilize its mounted point as an alternative to its path. For all those who would like to defragment the whole hard disk in Linux, they should use this command:
“sudo e4defrag /”
So, these were some of the ways to defragment hard disk in Linux and though there is a least guarantee of their success (that’s why I said to backup all the times) but trying them with care and cautious is worth it to clean up your hard drive in Linux system. However, the fact remains obvious that you won’t be bothered much about fragmentation in Linux as the file system is extremely efficient. So, enjoy using your all space in your Linux system.