Do ext4 filesystems need to be defragmented? If so, how do I defragment them?

If not, could you post a simple explanation of why they do not need to be defragmented?

  • 4
    If you are as interested as you sound, perhaps this paper would be of interest regarding this: Fedora Project, specifically starting at page 179 (which is really page 3) – nerdwaller Jan 15 '13 at 21:39
  • @nerdwaller Interesting, though a little to in-depth for me to fully comprehend. – BenjiWiebe Jan 15 '13 at 21:42
  • 1
    Related question on Ask Ubuntu: Why is defragmentation unnecessary?, where the current accepted answer is: The underlying filesystems used by Ubuntu, like ext2 and ext3, simply don't need defragmenting because they don't fragment files in the same way as NTFS. There are more details athttp://en.wikipedia.org/wiki/Ext3#Defragmentation. – Breakthrough Jun 14 '13 at 17:28

Do ext4 filesystems need to be defragmented?

Yes (but very rarely).

If so, how do I defragment them?

Copy all the files off the partition, erase the files from the partition, then copy the files back onto the partition. The file system will intelligently allocate the files as you copy them back onto the disk.

If not, could you post a simple explanation of why they do not need to be defragmented?

ext4 acts in a more intelligent way than merely adding new files into the next available space. Instead of placing multiple files near each other on the hard disk, Linux file systems scatter different files all over the disk, leaving a large amount of free space between them. When a file is edited and needs to grow, there’s usually plenty of free space for the file to grow into. If fragmentation does occur, the file system will attempt to move the files around to reduce fragmentation in normal use, without the need for a defragmentation utility.

Thanks to a Comment by @Green Reaper my attention has been drawn to e4defrag.

  • So there is no on-demand defragmenter software? – BenjiWiebe Jan 15 '13 at 21:42
  • 3
    @BenjiWiebe If I knew of one I'd say but please do not read into the fact that I do not know anything more than that I am ignorant! But bearing in mind I gather that by nature there will be no chance of fragmentation of an ext4 disk less than 80-95% occupied and that copy/delete/reinstate is recommended, why would anyone bother to create such software? – pnuts Jan 15 '13 at 22:44
  • 9
    A link to the source you copy/pasted from never hurts... ;) howtogeek.com/115229 – Christophe L Nov 17 '13 at 2:10
  • @ChristopheL Agreed. I do usually attribute but the page you mention does not look familiar to me (mind you, from nearly a year ago) so I wonder whether I picked up most of the wording from a forum that showed the quote but did not itself attribute it. – pnuts Nov 17 '13 at 8:14
  • 4
    The tool known as e4defrag can be used to defragment ext4. It is provided by e2fsprogs on most distros. ext4 also tries to avoid fragmentation, but it is not always successful. – GreenReaper Dec 15 '16 at 23:01

I have been using ext4 partition for over a year. I often ran out of space, moved a lots of files in and out etc etc... these things are bad for fragmentation and yesterday I checked for fragmentation for the first time and only 2 files (not %, just two files) were fragmented.

On the Windows partition that was used for about a year longer with much more free space available, I have 95% fragmentation.

So no, you really don't need to defragment ext4 and if you want to be sure, leave the default free space for ext4 (default is 5%, can be changed by ex2tunefs -m X).

  • 1
    How do you check for fragmentation? – Sparhawk Apr 26 '15 at 10:06
  • 13
    If you have an ext4 file system created with the extent option (it's the default in most recent distros), you can check fragmentation on it with e4defrag -c /path/to/check, and defragment it without umounting with e4defrag /path/to/check. But if you have enough free space you won't need to do so. – gerlos May 8 '15 at 8:46

protected by Breakthrough Jun 14 '13 at 17:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.