How to defrag partitions in Linux?


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.

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

5 comments, leave yours

Leave a Comment

Your email address will not be published.



  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Emilio said

    I think that in the first command, $ sudo e4defrag -c, we need to add a parameter to it for it to work, vg $ sudo e4defrag -c /

  2.   Linez said

    In Linux, with the ext4 file system, it is as necessary to defragment as to have an antivirus, unless you have a server with a lot of traffic, for a desktop user defragmentation is unnecessary, because the system is very rare that it goes beyond 1 % fragmentation, I have ubuntu installed from version 14.04, I have been updating until now, I am with 18.04, used for 4 years and the fragmentation is 0,4%. , I mean NULL.
    As a curiosity the article is fine, but I repeat that it is not necessary to do it on a personal computer and that you can do it if you get bored but your computer will not be faster and nothing will change much.

  3.   Someone said

    Hello, my comment has not appeared, so I repeat it.

    I am a systems administrator in a place where all computers use linux, most of them have been installed since 2007 and have been updated over the years to newer versions of the linux kernel. In no case, ever, has the disks needed to be defragmented, there is no fragmentation whatsoever, less than 1% in most cases.
    It is not true that over time they slow down, they go the same as always, there is no difference.
    In addition, with SSD disks it is even more unnecessary to defragment it, on the contrary, it is harmful to defragment it because it decreases the life time of the disk.
    That of defragmenting disks is something psychological inherited from Windows.

  4.   Someone said

    I just looked at some disks, randomly, and in all cases the fragmentation is 0 (zero).
    And they are disks that were installed, as I said before, in 2007

    Total / best extents 56635/55481
    Average size per extent 1821 KB
    Fragmentation score 0
    [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag]
    This device (/ mnt / wd1tb /) does not need defragmentation.

  5.   Raul said

    I believe that you are looking for justification in defragmentation by collecting programs that start with the system, as well as not performing periodic cleaning of temporary files, that this does encourage computers to have windows, linux or osx and that it is not so much in linux or osx due to fragmentation but there are more and more programs that consume ram and more information stored on the disk, to any of these two OS do periodic cleaning of startup programs and temporary files and you will have them working as newly installed, to windows not because this OS if it spreads the data all over the hard drive so it ends up looking for fragments throughout it, now that that is the advantage of using SSD disks in windows the problem of fragmentation is minimized because the search is not mechanical as in the traditional hard drives.