Over time our systems tend to get a little slow and this is largely due to the large amount of information that it handles. This information is stored within blocks which when we want to access it the disk head has to point to a certain position, now this is where the issue of defragmentation comes in.
Defragmentation is the convenient process by which files are arranged on a disk so that fragments of each of them are not appreciated, in such a way that the file is contiguous and without spaces within it.
Basically the system orders and has a mapping of the position of the files to optimize the operation of the disk.
Many will think that this is irrelevant to SSDs since they have nothing in common with the way HDDs read and write data.
For any of the cases, defragmentation is necessary, even if you use SDD, if you don't think that's the case, I recommend looking for a little more information on how to optimize and perform preventive tasks to improve the use of your SDD.
Although many say Linux file systems, mainly those based on extended versions or other journaling systems such as JFS, ZFS, XFS or ReiserFS, do not need defragmentation, the truth is that, over time, its operability ends up being slower due to the dispersion of the data.
Although the impact of this is never as dramatic as on FAT and NTFS based systems, it is something that we can easily resolve on the system, if we use a tool like e4defrag.
That is why, if you are not satisfied with the performance of your PC's disk and want to improve its integrity, we can perform the defragmentation of our disk in Linux in a very simple way.
Using the e2fsprogs utility for defragmentation
E4defrag is a utility that is available on most Linux distributions, including Ubuntu, inside the e2fsprogs package.
There are many other tools that work in a similar way, but e4defrag is quite simple to use.
To perform the task of defragmenting partitions in Linux, it is necessary to install the E4defrag tool. This tool is available in most Linux distributions, so to install it, simply search for it with your preferred software manager or search for it in the terminal in the repositories of your distribution.
In the case of Debian, Ubuntu and derivatives we install e2fsprogs with this command
sudo apt install e2fsprogs
In the case of Fedora, openSUSE, CenOS and derivatives we install with:
sudo dnf install e2fsprogs
In the case of Arch Linux and derivatives we install with:
sudo pacman -S e2fsprogs
Once the installation is done we can use the tool to use it it is important to point out that it is advisable to disassemble the devices or drives from your system where you will use this utility or something similar to avoid data corruption.
To use the tool, we must open a terminal and execute the following command:
sudo e4defrag -c
As a result, we will obtain an image that indicates a fragmentation value of our unit.
If this number reaches a score higher than 30, it is advisable to try to reduce it with the help of E4defrag, while if it exceeds the value of 60, it will be necessary to act as quickly as possible.
To defragment a drive, run the application with the following command replace / path / of / partition with the partition name:
sudo e4defrag /ruta/de/partición
O well, if you want to do the process on an entire device replace "Device" by the name of the desired device:
sudo e4defrag /rutadeldispositivo
We will only have to wait for our partition or unit to be successfully defragmented, the time this takes depends largely on the size of your partition or disk as well as the amount of information you have on them.
Without more, if you know any other tool for defragmentation of drives in Linux, do not hesitate to share it with us in the comments.