Is it necessary to defrag drives in Ubuntu? If so, how do I do it and how often should it be done?


Defragment is (or was) recommended under Windows because it had a poor filesystem implementation. Simple techniques such as allocating blocks for files in groups rather than one by one keep fragmentation down under Linux. Typical Linux filesystems only gain significantly from defragmentation on a nearly-full filesystem or with unusual write patterns. Most users don't need it, though heavy file sharers could benefit from it (filling a file in little bits in the middle is not the case ext3 was optimized for; if you're concerned about fragmentation and your bittorrent or other file sharing client offers that option, tell it to preallocate all files before starting to download).

At the moment, there is no production-ready defragmentation tool for the common filesystems on Linux (ext3 and ext4). If you installed Ubuntu 9.10 or newer, or converted an existing installation, you have an ext4 filesystem, which supports extents, further reducing fragmentation.

For those cases where fragmentation does arise, an ext4 defragmentation tool is in the works, but it's not ready yet.

Note that in general, the Linux philosophy and especially the Ubuntu philosophy is that common maintenance tasks should happen automatically without your needing to intervene.

  • 1
    This is a really nice answer Giles. Thanks for those links. – boehj Apr 20 '11 at 0:44
  • Thank you! I appreciate your prompt and informative answer -- especially with all of the links to help me further! – raucousloki Apr 20 '11 at 1:25
  • May I ask, for those of us that are heavy torrenters, which file system would best suit our needs for a dedicated torrent partition? Does anyone know? – boehj Apr 20 '11 at 1:38
  • 1
    @Tshepang: Some always preallocate, some allocate lazily, some are configurable. – Gilles Apr 20 '11 at 7:16
  • 1
    There's a tool called pydefrag as well which basically just copies around files so they get a chance to be rewritten contiguously, but it's probably only useful if you got fragmented from filling up the drive and now made enough space for those copies to happen. – maco May 10 '11 at 16:14

This is the best explanation (not only of file fragmentation but of how EXT2/3/4 is more resistant to it than FAT or NTFS) I have found and I still refer people to it: http://geekblog.oneandoneis2.org/index.php/2006/08/17/why_doesn_t_linux_need_defragmenting

  • Excellent. Thanks for the link, I'll have a read. – boehj Apr 20 '11 at 5:43

If you're using ext, here is a good quote from Theodore Ts'o (he was heavily involved in the development of the file system):

If you set the reserved block count to zero, it won't affect performance much except if you run for long periods of time (with lots of file creates and deletes) while the filesystem is almost full (i.e., say above 95%), at which point you'll be subject to fragmentation problems. Ext4's multi-block allocator is much more fragmentation resistant, because it tries much harder to find contiguous blocks, so even if you don't enable the other ext4 features, you'll see better results simply mounting an ext3 filesystem using ext4 before the filesystem gets completely full.

So really, the answer is 'no'. Only in extreme cases will you start to see adverse fragmentation (with ext).

  • No problem. I learned a lot out of this deal too! – boehj Apr 20 '11 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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