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
  • 2
    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
  • 15
    A link to the source you copy/pasted from never hurts... ;) howtogeek.com/115229 – Christophe L Nov 17 '13 at 2:10
  • 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
  • Is there a way to view the percentage of fragmented files? – Aaron Franke Oct 25 '17 at 18:26
  • Be aware that e4defrag only defragments files, not the free space. If your free space is fragmented, then e4defrag won't work well, because there's no contiguous space to move any files to. – jlh Mar 17 '19 at 9:39

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).

  • 2
    How do you check for fragmentation? – Sparhawk Apr 26 '15 at 10:06
  • 14
    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

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