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.
e4defrag /location
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 (if you have a separate ‘/boot’ partition, never use it on it!).
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.
man e4defrag
That’s it. Good luck.
22 Comments
There is GUI for e4defrag (i think):
https://apps.ubuntu.com/cat/applications/hdd-ranger/
Thanks ‘jeramia777’. I actually saw that too, but because it isn’t free, I couldn’t review it :).
excellently written – what a joy reading this nice, informative, relatively long/short article ; no need for a GUI in my eyes *** thank you so much and love & peace
I just buy it for my family Ubuntu laptop. It is working well, thanks for that link buddy!
Hi Gayan, great article!
A little typo: “and if you are using […] or never” — did you mean newer? 🙂
Thanks ‘Rafi’, I corrected it. I don’t know why, but it happens every now and then ;-).
Thanks for the howto.
Just a remark: running ‘e4defrag -c’ on the home folder gives no output (except for ‘Done.’), normal users are not authorized to see any. You have to use sudo even on your home folder to see the fragmentation count.
Hi Jan,
Thank you very much mate!. I updated the post :).
Thanks so much for this!!! I had a conversation with one of my brothers and I felt as thought it was unnecessary but now I get that it is, although less likely to be an issue than with Windows apparently.
You are welcome ‘Anthony’, & thank you for the link also :).
I stumbled upon this post while looking for info about e4defrag in DuckDuckGo> : it turns out it is not only the most up-to-date result (apart the Ubuntu man page>), but it is also an extremely easy to read and easy to understand guide for this little great program. Many thanks for that to the author ! 🙂
PS : sorry for the double post, I messed up with the HTML tags in the first one. Could you delete my previous comment and the notice in this one when it’s done? Thanks!
You’re welcome BT :).
It seems like the author of the article and the software does not understand how the “ext4” file system works. It is not inevitable that a file system has to be come cluttered and fragmented. It is fully possible to make a file system that does not clutter like NTFS. Now the Linux file systems will avoid fragmenting and cluttering, and will work in the background to clean up any mess, It is first when there are few spare blocks available that the file system will become cluttered – typically when you have used about 85% of the space on the disk. This is not Windows, and a lot of effort has been done to make the file system blistering fast. On top of that, the file system is journaled, so should the app crash or power plug pulled, the file system will always be consistent and come up consistent.. And “defragmentation” will not happen unless the device is more than 85% full.
For this part of the article:
sudo e4defrag -c ~/
sudo e4defrag ~/
..should the following be used instead? Since it is defragging the home dir and not the root dir.
e4defrag -c ~/
e4defrag ~/
Hi Torin,
If you want to defrag the root file system, then you should use it as below:
e4defrag /
If you want to get a more richer output (before and the after ‘fragment scores’ etc) then use it with the ‘-v’ argument:
e4defrag -v /
Please remember that, be very careful while using this utility with administrative (root) privileges, it can cause you troubles!. For instance, if you have a separate ‘boot’ partition, and if you degrag that (by specifically pointing to it: ‘e4defrag /boot’), then your system might fail to boot!. but I’ve used it to defrag the root file system and it haven’t given me any issues, and it simply ignores the files (say the ones that are associated with system boot) that shouldn’t be moved.
P.S: I put in the article not the ‘-‘ sign, but ‘~’ which refers to the currently logged in user’s home directory.
Hi Gayan.
But if I only want to defrag my home directory — must I still use sudo?
No, you can (and should) use it without ‘sudo’ :).
Thank you Gayan for clarifying that.
You’re welcome Torin.
Do I do something wrong or do I have to very… VERY patient?? I entered the command “sudo e4defrag -c /” and nothing happens except for a blinking terminal cursor. I am using Linux Mint 17.1. Any advice what I should do?
Hi,
When you use the ‘-c’ argument, ‘e4defrag’ simply scans the system and gives you a fragmented count, and that’s all it does. To defrag, use ‘e4defrag’ without it (‘-c’). If you prefer a more rich output, then use the ‘-v’ argument. For instance:
sudo e4defrag -v /
It is posible to use this command on chromebook? on my chromebook this command don’t work(this filesystem is non ext4). In ubuntu work corectly!