I don't want any answer saying that there's no need, please!
Use e4defrag to defrag your files
If your ext4 file system is created with the
extent option (it's default in recent distros), you can use the
e4defrag utility to check and defragment it online i.e. without umounting.
Just check fragmentation level with something like this (you need to be root to see details):
sudo e4defrag -c /path/to/myfiles
Here's an example of the output you can get:
$ sudo e4defrag -c iso/ <Fragmented files> now/best size/ext 1. /home/gerlos/iso/debian-live-7.5.0-i386-rescue.iso 7/1 111177 KB 2. /home/gerlos/iso/systemrescuecd-x86-4.4.1.iso 4/1 100897 KB 3. /home/gerlos/iso/debian-live-7.5.0-amd64-rescue.iso 6/1 116053 KB 4. /home/gerlos/iso/ubuntu-14.04.2-server-amd64.iso 8/1 76160 KB 5. /home/gerlos/iso/ubuntu-14.10-desktop-amd64.iso 15/1 75712 KB Total/best extents 40/5 Average size per extent 90577 KB Fragmentation score 0 [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag] This directory (iso/) does not need defragmentation. Done.
As in this example, most of the time it will tell you that no defragmentation is needed, but if you want to do it anyway you can use (you don't need to use sudo to defrag your own files):
Your users can even run it on their own files, there's no need to be root unless you want to work on other user's or system files.
e4defrag is in the
e2fsprogs package, and I guess it's already installed on your Ubuntu system.
Take a look on e2fsprogs. This is also available in the Ubuntu packages. It provides the program e4defrag.
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.
Also, note that you need to do as super-user only if you want to defragment without unmounting.
e4defrag -vc /path #verbose output with count
Ubuntu 14.04 LTS does place (at least the primary drive) into EXT4 partition when installed on a clean install (not residing on top of Win-Junk).
In this case, if you have just set up a new system, or formatted fresh on an old system and then did a clean install, then by default with 14.04 LTS, you have an EXT4 Partition at
Now, just open a Terminal (CTRL-ALT-T) and type:
sudo e4defrag -c /dev/sda1
and be patient until the 'check' is completed and you get the fragmentation report back.
If you aren't confident about having an EXT4 Partition on your OS drive, simply go HOME and type
df -T. Look to see what your sda* partitions are set to (try this in 14.04LTS, and you'll see EXT4 unless you chose another partition type upon harddrive setup).