Different operating systems use different file systems. The GNU/Linux for instance, has a lot of file systems, but primarily relies on the ‘Ext4′ (which will eventually be replaced by ‘Btrfs’). And when talking about file systems, another thing that comes into mind, as it has a direct effect on the performance of the file system, is the term, ‘fragmentation’.
Because, no matter what file system that your operating system uses, it is inevitable that it too will be fragmented, as time goes on. And the main reason for file system fragmentation is the complications that occur while trying to find ‘nearby’ or ‘continuous’ free space areas (‘blocks’).
This is because, if it was a program, and the OS was able to store its data in nearby blocks, then while opening it later, finding those blocks that hold the program on the HDD is easy, as they are, nearby (‘continues blocks’).
But, while trying to store the program, if the OS was unable to find nearby blocks and its data got scattered all over the HDD (which is a common scenario as you add/remove data, unlike in a newly formatted partition, it destroys the continuum of the free space), then it will take more time to load as the HDD’s ‘head’ (the mechanical tool that read/write data to sectors) has to look all over the place for finding (reading) them.
Some say the GNU/Linux file systems, such as ‘Ext4′, does not need any fragmentation (unlike with Windows file systems), as it is designed in such a way, it minimizes the fragmentation.
This is, to some extend, is true. But, there are experts like ‘Takashi Sato’ who has done many experiments using few major GNU/Linux file systems and their file fragmentation’s effect on the performance.
He has written an excellent PDF called ‘ext4 online defragmentation’ (a more up-to-date one can be found here) and makes the following conclusions at the end:
Fifty fragmented 1GB files were created (Edit: using ‘ext4′). Read performance was measured before and after defragmentation …
… Performance measurement has shown that defragmentation for a single file can improve read performance by 25% on a fragmented 1GB file. For relevant file fragmentation, defragmentation resulted in 29% performance improvement for accessing all file in the Linux source tree.
Online defragmentation is a promising feature to improve performance on a large filesystems such as ext4 …
He and ‘Akira Fujita’ has created a simple and easy to use command-line based tool that lets us defragment any ‘ext4′, for improving the performance. It is called ‘e4defrag’ and if you are using Ubuntu 12.04 LTS, 12.10 Quental Quetzal or newer, then it is already installed in the OS.
But remember, this only works in the ‘Ext4′ file system and not in any other.
How to use it?
Very simple. Open your Terminal and enter the below command.
The ‘location’ can be anything from a mounted ‘ext4′ device (including the root file system, ‘/’) to any individual file or a folder inside an ‘ext4′ file system.
Getting a ‘report’ …
Another cool feature of ‘e4defrag’ is that, it also has the ability to scan a mounted ‘ext4′ file system (or any location inside it) and give you a ‘report’ that includes a value called ‘fragmentation score’.
And based on this ‘fragmentation score’, ‘e4defrag’ tells you whether you should defrag that location or not!.
Note: If the score in the output is within 0-30 then you do not need to defrag. If it says 31-55, then it is a bit fragmented and if it is 56, then you should fragment the location.
For that you can use it in the below format (make sure to replace ‘/location’ with your file path).
e4defrag -c /location
Here are few examples …
Using it on the ‘Home’ folder …
If you wanted to get a ‘fragmentation score’ for the ‘Home’ folder first and then defragment it later, then use the below two commands.
sudo e4defrag -c ~/
sudo e4defrag ~/
(Here, there’s no need to change anything, the ‘~/’ will automatically add your ‘Home’ folder path).
Using it on the ‘root’ file system …
If you wanted to get a ‘fragmentation score’ for the root file system (including the OS), and defrag it later, then use the below two commands.
sudo e4defrag -c /
sudo e4defrag /
Note: While the defragmentation is done, it will give you a basic output. And in this output, it is normal to have values under ‘Failure’, as some of the core system files are unmovable.
Please remember that, in GNU/Linux, all of your files are mounted on the ‘root filesystem’ (‘/’). So if you had more than one ‘ext4′ file systems mounted, then they too will be defragmented, thus, depending on their files and their sizes etc, it might take a long time.
Deframenting mounted ‘ext4′ partitions manually?
If you have more than one ‘ext4′ partition and wanted to defrag them individually, but don’t know their ‘device name’ (/dev/sda …), then you can use the below steps for that.
1. Enter the below command in your Terminal to get a list of current partitions and their file systems.
sudo fdisk -l /dev/sda
2. Then in the output, under the ‘Id’ field, if for a partition it says ’83′, then that is an ‘ext4′ file system. Then for that line, look under the ‘Device’ and whatever lies there, is the device location of that ‘ext4′ partition (in this example, I have two, ‘sda5′ and ‘sda6′).
3. Then simply copy that name under ‘Device’, and replace it with the ‘sda’ part in the below command.
sudo e4defrag /dev/sda
Depending on the partition (whether its where the OS is installed etc), you might or might not need to use ‘sudo’.
If you want to know more, then you can read the short manual of ‘e4defrag’, by using the below command.
That’s it. Good luck.