How to defragment in Linux

Defrag banner on linux

Although it has always been a rumor that Linux file systems, based mainly on versions of extend or other systems with journal like JFS, ZFS, XFS or ReiserFS, they do not require defragmentation, it is true that over time its operation is slowing down due to the dispersion of data. Although its impact is never as dramatic as in FAT and NTFS based systems, it is something that we can easily solve within the system if we use a tool like e4defrag.

E4defrag is a utility that is available in most Linux distributions, including Ubuntu, within the package e2fsprogs. There are many others that operate in a similar way, but we have chosen this for its ease of use. In order to install it within our system, it is only necessary to invoke the following command:

sudo apt-get install e2fsprogs

Once the package is installed, we can invoke the utility from the command line by executing the following statement:

sudo e4defrag -c

As a result we will obtain an image similar to the following one that indicates a fragmentation value of our unit. If this figure reaches a score higher than 30 it would be It is recommended to try to reduce it using the utility that we have indicated, and if it exceeds the value of 56 it will be necessary to act as soon as possible.

Look at the e4defrag utility

To defragment a unit we must invoke the application with the following sequence:

sudo e4defrag /ruta

Or this other one if we want to act on an entire device:

sudo e4defrag /dev/device

As always, we remind you that it is advisable to disassemble the devices or drives of your system on which you are going to act with this utility or a similar one to avoid data corruption.

Finally, theWe encourage you to leave your comments and tell us what This application has worked so well for you and if you have noticed any improvement in your computers after running it.


19 comments, leave yours

Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  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.   alice nicole saint said

    what is gained by grabbing that !!! speed or something?

    1.    Luis Gomez said

      Hello Alicia, indeed, the location of the data makes that in the same pass the head of the disk catches the information that is going to be used later and therefore the memory pages that are going to be used are hit. This translates into higher speed.

  2.   alicia nicole san said

    How do I defragment my ubuntu if this tool is in use. It says that it has to be disassembled I don't understand

    1.    Luis Gomez said

      Hello Alicia, review the umount command and apply it on the drive or device that you are going to defragment. A typical example of umount is with the CDROM: umount / dev / cdrom.

      A greeting.

  3.   RioHam Gutierrez Rivera said

    In Windows, defragmenting helps to find files faster. Imagine a shelf full of books, all together. Removing one leaves a void. That happens on the hard drive when we delete a file. This has the effect that the system is a little slower due to the fact that it wastes time searching, even in those gaps. Defragmenting serves to gather the information and not be empty. In Linux it does not cause a great impact as in Windows. But it can be good if we have been using it for a long time.

  4.   alice nicole saint said

    oh ... I understand thank you. if I had some knowledge but in windows. but in linux it grabs me too fast more than linux .. even if over time it grabs a little slow not like windos now I have it very slow I think that is for eindoes 🙂 I have installed the disk win and linux. thanks for the info

  5.   fedu said

    I have a kingston usb 3.0 memory The one I used to install ubuntu, but one day I don't know what happened, if it was that I removed the memory without unmounting it or I don't know but from that day on it was "read only" and since then I have wandered through the pages to see if I can recover this memory (because it is high speed usb 3) but nothing, as they say in Spain «na de na», does anyone know how to fix it, or at least explain how to prevent this from happening again?

    1.    Rowland Rojas said

      Have you tried erasing your data with Gparted?

    2.    dextreart said

      There are several ways you can use an installed application called Open Disks and you locate the usb that is in and you give it fomat, another option would be through the terminal

  6.   Miguel Angel Santamaría Rogado said

    Hi Luis,

    I'm sorry to tell you that the article has been a bit imprecise.

    On the one hand, it is not time that causes fragmentation in file systems, but usage patterns: creating thousands of small files and then randomly deleting some, writing very large files very slowly, etc .; and the degree of occupancy of the filesystem, a use above 90% is mentioned as a point at which a filesystem is not capable of mitigating fragmentation (although I have never seen a formal explanation of that 90%).

    On the other hand, the commands you have put are changed: "e4defrag -c / path" shows the information (count) about the fragmentation and "e4defrag / path" does the defragmentation.

    To finish, I leave here [1] an article that explains in a fairly simple way a topic as complex as that of file system fragmentation; It is from 2006 and does not mention structures or methods such as "extents" or online defragmentation, but it is easy to understand.

    Greetings.

    PS: Just out of curiosity, to indicate that after a year and a half of use and without defragmentation of any kind, my system has a brand new 0% fragmentation at 79% of use (Ubuntu 14.04).

    [1]: http://geekblog.oneandoneis2.org/index.php/2006/08/17/why_doesn_t_linux_need_defragmenting

    1.    Luis Gomez said

      Hello Miguel Ángel, first of all, thank you for the note. I modify the sentence right now. As you well indicate, the usage patterns and even before that, the choice of the cluster or block size, will later condition this behavior in the units. As it is not foreseeable if we will have many small files or few and large files in our unit, the default value that the system handles is usually taken.

      On the other hand, indicate that the gain of defragmentation is not so much in the compaction of the information as in the good order followed by the information. The less the heads of the disk have to jump, the more speed we will gain (and in general it tends to happen with large files and with blocks in a row than with many small ones located randomly on the disk).

      Thanks for reading.

  7.   zytumj said

    Total / best extents 276635/270531
    Average size per extent 252 KB
    Fragmentation score 0
    [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag]
    This directory (/) does not need defragmentation.
    Done.
    --------------
    The computer is about 3 years old, not bad at all, right?
    Linux Mint 17.2

    1.    Miguel Angel Santamaría Rogado said

      Hello zytumj,

      that fragmentation is practically non-existent is normal in the file systems used in Linux, "they are thought" to avoid it.

      It's really not worth defragmenting in Linux, these tools are mainly just in case you need to do some kind of resizing of the partitions, so that you don't have files at the end of the partition that don't allow you to change the size.

      Greetings.

      PS: I didn't mention it before and neither does the article, but if you have an SSD disk, defragmenting it is a waste of time regardless of the file system you use.

  8.   zytumj said

    Thanks Miguel Ángel.
    No, I use a traditional disc. Likewise, when I started with GNU / Linux back in 2008, I already looked for how to defragment and I read that it was not necessary.

    1.    unknown channel said

      Since they touch on the subject of the files distributed throughout the partition and it is thought to make a reduction of the partition. I have denoted that using graphical applications such as Defraggler or another from windows for NTFS partitions on an HDD, many times they cannot defrag enough, and when they do, there may be files left towards the end of the partition.
      I wonder if in Linux there can be a 0% fragmentation in an Ext4 partition, but also there are files towards the end of the partition, that is, towards the center there is empty space.

      I think, the ideal of a data save in a partition, is that the data be saved towards the center of the partition towards the outside. What do you think?

  9.   leonardo said

    Hello. And how can I defragment NTFS or FAT32 partitions? Thanks

  10.   Patrick said

    Hello everyone! I've been using Ubuntu for years and it has never taken a long time, I adore it. 10 seconds to start and 3 to shut down. Greetings!

  11.   elianne said

    I work with three printers and none of the three I can install in Ubuntu 20.04, I already downloaded the drivers for each of them. the pc is new and ubuntu is just installed. with the previous pc which I had to discard because it did not start (initramsf) and nobody was able to repair it, all three printers worked well. the printers are two epson and one hp.
    lsb does not exist in ubuntu 20.04

  12.   Enrique said

    Good afternoon.
    To use e4defrag it is necessary that the device is mounted:

    root@Asgar:/media# umount disk1
    root@Asgar:/media# e4defrag /dev/sda1
    e4defrag 1.46.6-rc1 (12-Sep-2022)
    Filesystem is not mounted
    root@Asgar:/media#

    Greetings.