If you are interested in how the Linux filesystem works, then this is a posting you really need to read: http://geekblog.oneandoneis2.org/index.php/2006/08/17/why_doesn_t_linux_need_defragmenting. This is a posting explaining how the Linux filesystem stores file and why it does not need to defragment the filesystem as it stores files more efficiently. I have never wanted to defragment a Linux filesystem and I have used quite a lot of Linux distributions. The filesystem stores files more efficiently than a FAT or NTFS filesystem does. There is an answer in AskUbuntu that gives some insight into how you can defragment a Linux filesystem, but this is not something that you really need to do. Linux filesystems, being more efficient are more reliable as a consequence and do not need as much attention as a Windows filesystem would. Although you do not need to defrag as much as you used to with Windows `98, and you do not get the cool window with the blocks moving around like you did in the olden days of Windows. That would make a cool screensaver though. There is more information here about the defragmentation of Linux filesystems and how this is handled by Linux transparently. The key seems to be maintaining a certain amount of free space on your Linux partition to allow the files to move around.
The default free space recommendation for ext4 is 5%, if you have at least this amount of space, your partition will not get fragmented. But with the size of modern hard disks reaching 3 terabytes, this should not be too difficult. You may run the below command to assess the level of fragmentation on your hard drive.
[email protected]:~$ sudo e4defrag -c /dev/sda5 <Fragmented files> now/best size/ext 1. /var/log/pm-powersave.log.1 20/1 4 KB 2. /home/homer/.cache/chromium/Default/Media Cache/index 7/1 4 KB 3. /var/log/pm-powersave.log 25/1 4 KB 4. /var/lib/lightdm/.cache/upstart/indicator-datetime.log 6/1 4 KB 5. /var/log/wtmp 45/1 4 KB Total/best extents 205081/204017 Average size per extent 85 KB Fragmentation score 0 [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag] This device (/dev/sda5) does not need defragmentation. Done.
Then you may run this command to defragment your hard drive.
[email protected]:~$ sudo e4defrag /dev/sda5
This does work very well, but you will not need to run this often.