Tutorial: Defragment your hard drive under GNU Linux

Defragment Linux

La great robustness of file systems (FS) that are used in GNU / Linux systems has made users fall into an error, thinking that it is not necessary to defragment the hard disk as it happens in Windows. This is not entirely true, although it is not as necessary and vital as in Microsoft systems, it is good to do it.

The systems ZFS, EXT, JFS, XFS, ReiserFS, Btrfs, etc., they have intelligent allocation systems for the files that avoid that sharp and horrible fragmentation that occurs in Windows, but that does not mean that the problem does not exist. Years can pass after the installation of your distribution and fragmentation would not be a problem, even if you use it intensively and install and uninstall libraries, etc.

However, some users with limited hard disk space they may be more marked by the problem of fragmentation. Limited space makes it more difficult for these filesystems to allocate spaces to new files. That is why I recommend installing a tool to defragment your hard drive. There are many, such as e4defrag (which you probably don't even have to install because it is already installed by default in your distribution).

Its use is very simple, you just have to open a terminal window and type the following:

sudo e4defrag -c /ruta

Replace / path with the partition or device you want to defragment. For example: “sudo e4defrag -c / dev / sda1” or it can be a single folder “sudo e4defrag -c / home”. But be careful, this will only tell you the amount of files that have been found fragmented. If the number is low you have no reason to defragment, but if it goes above 30 you should consider defragmenting. To defrag all partitions:

sudo e4defrag /dev/sda*

In case you only want to defragment a specific directory, you can use for example "/ home" instead of "/ dev / sda *" or even specify the partition "/ dev / sda5". If you have an SSD, you don't have to worry from this, defragmentation only makes sense on magnetic hard drives ... SSDs are so fast that it is not worth it and also, defragmenting only increases the read / write cycles and therefore decreases the useful life of your drive.

Leave a Comment

Your email address will not be published. Required fields are marked with *



  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.   Y3R4Y said

    Thanks for keeping us informed, I did not know about the e4defrag application.

    A greeting.

  2.   Ricardo said

    As far as I know, e4defrag is only for ext4 file systems. Greetings

    1.    Isaac PE said


      You are right. I did not put it, but it is a tool included in the e2fsprogs package and it is for EXT4. For other FS (although not for all) there are also utilities for this. For example for Btrfs:

      btrfs filesystem defragment "directory"


    2.    Raphael Linux User said

      I was going to make the same comment. It is very important that you correct it, since it takes for granted that we all use Ext4, and today, it is a system to be phased out in computers with less than 4 years that support it without impairing its performance. BTRFS and XFS are ideal, and essential for storing 4K movies.

  3.   Danny King said

    e4defrag - online defragmenter for ext4 filesystem

    e4defrag [-c] [-v] target ...

    e4defrag reduces fragmentation of extent based file. The file targeted by e4defrag is created on ext4
    filesystem made with "-O extent" option (see mke2fs (8)). The targeted file gets more contiguous blocks and
    improves the file access speed.

    target is a regular file, a directory, or a device that is mounted as ext4 filesystem. If target is a direction
    Tory, e4defrag reduces fragmentation of all files in it. If target is a device, e4defrag gets the mount point
    of it and reduces fragmentation of all files at this mount point.

    -c Get a current fragmentation count and an ideal fragmentation count, and calculate fragmentation score
    based on them. By seeing this score, we can determine whether we should execute e4defrag to target.
    When used with -v option, the current fragmentation count and the ideal fragmentation count are
    printed for each file.

    Also this option outputs the average data size in one extent. If you see it, you'll find the file has
    ideal extents or not. Note that the maximum extent size is 131072KB in ext4 filesystem (if block size
    is 4KB).

    If this option is specified, target is never defragmented.

    -v Print error messages and the fragmentation count before and after defrag for each file.

    1.    Claudia Segovia said

      Sorry but I don't know what file system my Ubuntu 16.04 has and I don't know how to see it.
      Does anyone have an idea or could help me?
      It is to know if I can use this system or another (or neither).
      From already thank you very much.

  4.   Benedict said

    is it convenient to use on ssd disks?


    No Benito, in the post it indicates that it makes no sense to apply it to SSD disks, since it shortens their useful life.

  6.   M said

    Something similar to this is possible to apply it from… ubuntu to defragment the NTFS system with windows? I understand that windows has files that cannot be moved but what if it is allowed does not do it completely, I tried with various programs but a few have done a good job but not enough to be able to move those files that prevent the resizing of the system that it would be easier to delete both and resize to my liking but there is software in windows that I no longer have a license