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.