Do ext4 filesystems need to be defragmented?

46

6

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?

BenjiWiebe

Posted 2013-01-15T21:20:48.253

Reputation: 7 672

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 – 2013-01-15T21:39:04.547

@nerdwaller Interesting, though a little to in-depth for me to fully comprehend. – BenjiWiebe – 2013-01-15T21:42:53.813

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 – 2013-06-14T17:28:16.417

Answers

31

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.

pnuts

Posted 2013-01-15T21:20:48.253

Reputation: 5 716

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 – 2016-12-15T23:01:54.143

Is there a way to view the percentage of fragmented files? – Aaron Franke – 2017-10-25T18:26:03.153

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 – 2019-03-17T09:39:46.937

@ChristopheL From what I see, the source you claimed to be has the timestamp "SEPTEMBER 28, 2016, 11:00AM EDT", while this answer is written at "Jan 15 '13 at 21:35". What amazed me is how you were able to foretell the URL of a post 3 years in advance? – zypA13510 – 2019-06-27T08:43:27.763

Wouldn't scattering the files increase random access times? – Tooniis – 2019-12-25T05:05:33.300

@zypA13510 - first Wayback capture shows publication date of 30-May-2012

– Tom Goodfellow – 2020-02-25T08:17:43.533

@Tom. Thank you. I agree far too much (almost all) the wording is identical and How-to Geek has precedence. I'd like this A removed. – pnuts – 2020-02-25T17:51:21.137

So there is no on-demand defragmenter software? – BenjiWiebe – 2013-01-15T21:42:26.123

10

A link to the source you copy/pasted from never hurts... ;) http://www.howtogeek.com/115229/

– Christophe L – 2013-11-17T02:10:27.063

20

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

zahjin

Posted 2013-01-15T21:20:48.253

Reputation: 203

1How do you check for fragmentation? – Sparhawk – 2015-04-26T10:06:30.073

13If 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 – 2015-05-08T08:46:38.193