How to defrag your Linux system

There is a common misconception among GNU/Linux users that our systems never ever need to be defragmented. This stems from the success of the journalized filesystems used by most distributions including EXT2,3 and 4, JFS, ZFS, XFS, ReiserFS and BTRFS. All of these boast smart ways and techniques in regards to the files allocation in the disks, minimizing the fragmentation problem to a point that there is practically no reason to defrag even after many years of installing and uninstalling applications and libraries in the same system. Fragmentation though can still be an issue though, especially for users that use space limited disks that may not offer many file allocation options.

Here's a bulk description of how the (Linux) file allocation procedure works: files are stored in multiple places in the disk, leaving huge unwritten space between them, allowing them to grow unobstructed over time if needed. This is in contrary to filesystems like the Windows' NTFS which places files next to each other consecutively. If the disk gets more crowded and a file needs more space to grow by staying in one piece, Linux filesystems attempt to re-write it completely on another sector that has enough space to store it as a whole. This way, everything is kept clean, tidy and in one piece each. Confined space though, causes this file “maneuvering” to get more challenging with time. Here's how to deal with this problem and how to actually defrag your Linux system.

Now, the first thing that you'll need to do is get a defragment tool installed. There are many defragmenters available for Linux filesystems but I will use “e4defrag” as it is one that will most probably be already installed in your system. Using this tool, you can determine if you have fragmented files and how serious this fragmentation is. To do this, open a terminal and type: sudo e4defrag -c /location or /dev/device. Below, I have scanned my /home folder for fragmented files and actually found five of them. My fragmentation score though is quite low so defragging won't do much different in my system's performance in that case. If this outputs a score over “30”, then defragging would be a good idea.

At first, I will demonstrate how to defrag using e4defrag, by defragging my fragmented files one by one. The e4defrag program is part of the e2fsprogs package which should already be installed on your computer. In case the program is missing, install it with this command on Ubuntu:

sudo apt-get install e2fsprogs

To do this I use the following command:

sudo e4defrag

followed by the location and name of the file as shown in the screenshot below:

This is good when you have to defrag just a couple of files like I did, but if you want to defrag your whole system then you should first unmount all partitions and run the following command:

sudo e4defrag /dev/*

If you want to perform defrag without unmounting, then:

sudo e4defrag /

would be a safe choice.

Since many users nowadays use SSDs and not HDDs, it is important to note that the defragmentation procedure is only beneficial for the later. If you own an SSD, there is simply no point in worrying about fragmented files as those disks can access their storage randomly, wheres HDDs access sequentially. Defragging your SSD will only increase the read/write count and thus reduce the lifetime of your disk. SSD owners should convey their interest on the TRIM function instead, which is not covered in this tutorial.

Share this page:

Suggested articles

14 Comment(s)

Add comment


From: Curtis

If you are a btrfs user, you can use the btrfs command to defragment.  This works on a directory level and has to be told to defrag recursively.  So if you wanted to defrag the whole system, you would do something like:

     #  btrfs filesystem defragment -r /

From: Van

Thank you for this and nice wallpaper.

From: Davide Repetto

Actually "sudo e4defrag /dev/*" will not work. Partitions need to be mounted for e4defrag to do its work.

From: Scott

I just ran this on my /home partition, which was created more than three years ago when I first got this computer.  I have re-installed or upgraded the OS in a separate partition many times since.  Out of tens of thousands of files only five are fragmented.  I have never heard credible stories from any source claiming that their Linux partitions were in need of defragmenting, and this check of my /home partition just adds credibility to the claim that Linux filesystems do not need defragmenting.  It's nice that there are tools available should that ever become necessary, though.  I can understand this need if someone is dedicating their computer to something like a database and after many thousands of transactions it becomes somehow fragmented and degrades performance.  But this demonstration flies in the face of your opening claim that it is a misconception on the part of Linux users that their systems do not need to be defragmented.  In the days of MS-DOS it was common to dedicate an evening to defragmenting the FAT filesystem.  It would take hours on "large" (60+ MB) partitions to defragment them.  If only they would just stay defragmented.  The Norton Utilities were a best-seller in large part because they included a defragmenting program.  But for Linux, I see this utility as an effort in completeness rather than necessity.

From: Bill_Toulas

There are use cases such as "extensive use of bittorrent tools" that may result in a lot of fragmented files. In most common scenarios though, yes there is no reason to defrag. That's what I wrote in the very first paragraph: "minimizing the fragmentation problem to a point that there is practically no reason to defrag even after many years of installing and uninstalling applications and libraries in the same system" which is exactly what you describe as well. :)

From: Gene

Totally off topic. What DE are you running? Is that Gnome Shell? On top of Fedora? Looks much better than the last time I checked it out...

From: Bill_Toulas

The DE is a GNOME-Shell fork called Panthen developed by Elementary OS developers. 

From: present_arns

Nope that looks like ElementaryOS :)

From: tehnikpc

Not relevant, because all servers have long been SSD.

From: till

Most servers still use normal hard disks, especially when you have to store large amounts of data.

From: Bryan

It should point out that defragging an SD card is important if you know the contents are less than, say, 4G and you want to copy that content to a 4G device.  For example, I have an 8G sdcard.  I know this card contains less than 4G of data.  I want to move these contents to a 4G flash part using 'dd'.   DD will fail, obviously, but at least it'll copy the contents over (only to require a little fsck fix up later, no biggy).  But, if the 8G part is not defragged data could exist at the 4G+ space and will never get copied over.

From: david

This is horrible advice, please nobody think that using dd from a larger device to a smaller one is ever a good idea.

If you really want to do it, first shrink the partition to match the smaller device, then dd just the partition, not the entire device.

From: don

It would have sure been nice if fonts in the picture of your screen had been large enough to have been readable!

From: Scott

It is a common misconception, but Pantheon is not a fork of Gnome Shell.  "...notable difference between GNOME Shell and Pantheon is choice of programming language. Pantheon is written in Vala, while GNOME Shell is written in javascript (Specifically, GJS). Just to reiterate, these are totally separate unrelated code bases and are not derived from each other at all. We’re committed to using a single language across our entire desktop and apps. This allows us to share code, enforce a consistent code style, and ensure developers writing their own apps can learn from and adapt our code to new purposes."