Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I don't want any answer saying that there's no need, please!

share|improve this question

3 Answers 3

up vote 10 down vote accepted

Use Gparted to defrag your file system

If you have enough space on your hard drive, you can use Gparted to defrag your file system (ext2, ext 4, nfts, etc.). You have to boot from a CD/DVD/USB boot disc because the drive you're working on has to be unmounted. You also have to have more unused space available than used space for this to work and it may take a while.

  • Boot from a boot disc.
  • Run gparted and shrink the partition that contains the data you want to defrag to just over the amount of your data.
  • Make sure the partition you want to defrag is the last partition on the drive by moving it to the end (you may have to create another blank partition in front of it if there's only one partition on the drive).

With the partition you want to defrag as the last partition on the drive:

  • grow the partition to the left of it to maximum size. This will move your partitions data to the end of the drive.
  • Once it's done, shrink that partition back to it's previous size.
  • If you created a blank partition to have more than one on the disc, you can now delete it.
  • Move your partitions back into the original order and regrow the partition you wanted defragged back to it's full size.

It's now defragged.

And I know you're not interested in why/why not to defrag ubuntu, but I'll post the link to why-is-defragmentation-unnecessary anyway.

share|improve this answer
After shrinking and moving right, why do you grow left then shrink again instead of just moving left? –  AKS Oct 22 '13 at 17:47
Also I noticed that after the grow-left step, the data itself was actually moved left again (evidenced by gparted moving data, verified by a direct examination of the block device afterwards), so unless that is version/filesystem-dependent behavior the final shrink + regrow can be skipped. It appears to be sufficient to only do: 1) shrink and move right, 2) apply, 3) grow left. –  AKS Oct 22 '13 at 18:59
Simple moving the partition you want to defrag to last does not necessarily move it to the actual end of the HD. Growing the partition to the left to maximum size makes sure that the end partition is actually "moved" to the very end of the hard drive. –  James Oct 22 '13 at 22:04
Moving it to the right does move it to the end of the hard drive. The partition bar at the top of the gparted GUI is actually an accurate representation of the data layout (well, at least as visible to the OS, flash devices for example map logical to physical locations on the other side of the controller, but for a typical HDD, it is accurate). This can be verified by observing the new start location in the partition table. :) –  AKS Oct 22 '13 at 23:03
Ah, we are miscommunicating. You meant reorder the partition so it is last. I meant move the partition to the end of the drive, and that's what I thought you meant as well (you can move partitions around on the drive with gparted independently of reordering them). :) –  AKS Oct 22 '13 at 23:05

Take a look on e2fsprogs. This is also available in the Ubuntu packages. It provides the program e4defrag.

share|improve this answer
This is really only useful on ext4 file systems that were created with -O extent, which is not the default. –  dobey Nov 23 '12 at 20:43
The guy answes about defrag, it could be posible with e4defrag, the cuestion not specity an external journaling or not, he don't deserve a -1. –  Felipe Alcacibar Jan 8 '13 at 23:25
@dobey Ubuntu 12.04(.3) LTS contains extent in the ext4 stanza of the [fs_types] section in /etc/mke2fs.conf - thus, it is a "default". Though, regardless of the create-time configuration of the machine to which the filesystem is connected, you're best off to inspect the filesystem itself: tune2fs -l </dev/with/ext4> | grep extent (or look at the "Filesystem features" line in the complete tune2fs output). Mind you, I just ran e4defrag -c ... on a 100% full 1TB filesystem with extent, and there are only 5 fragmented files, and "fragmentation score" 0. YMMV. –  Richard Michael Dec 28 '13 at 0:30
  1. don't fill your filesystem more than 80% if possible, and certainly not more than 90%. defragging or no defragging, that is just BAD for your disk performance, pretty much period. if they're that full - fix that. your hard drive needs breathing room. if you don't give it breathing room, your performance will suck. this is a constant.

  2. copy files elsewhere. delete originals. copy original files back. presto, minimal fragmentation. ASSUMES STEP ONE IS TRUE

Basically, it is possible to end up with badly fragmented files, even with relatively low overall fragmentation - bittorrent clients are notorious for this - but it's relatively uncommon (bittorrent clients are about the only thing that does this), and it's usually easiest to fix it just by copying the files. A few fragments are fine; 4,000+ fragments in a single file is not fine.

Oh, yeah -

  1. DON'T EVER DEFRAGMENT AN SSD. And, y'know, spinning platters of rust are on the way out anyway, so... seriously, best to just get out of the habit.
share|improve this answer
Isn't bad performance with disk usage more of a Windows problem? –  NoBugs Jul 21 '13 at 6:27
yes and no. ext4 is somewhat better than ntfs at avoiding fragmentation, but it can still happen. bittorrent clients are notorious for producing HORRIBLY fragmented files, and if you let your filesystem get more than 80% full, you'll start ending up with more and more badly fragmented files as the filesystem no longer has any other CHOICE but to store new files in small fragments of what little free space is available. –  Jim Salter Aug 8 '13 at 19:45
So fragmentation is the only thing that would make disk usage a performance problem? –  NoBugs Aug 9 '13 at 3:20
I have no idea what you're trying to get at here. Try being more explicit. –  Jim Salter Aug 9 '13 at 18:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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