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.
jason@Yog-Sothoth:~$ sudo e4defrag -c /dev/sdb6 e4defrag 1.43.5 (04-Aug-2017) <Fragmented files> now/best size/ext 1. /media/jason/2253b90a-2608-4939-917d-85e1dbcbeebb/jason/.profile 1/1 4 KB 2. /media/jason/2253b90a-2608-4939-917d-85e1dbcbeebb/jason/.bashrc 1/1 4 KB 3. /media/jason/2253b90a-2608-4939-917d-85e1dbcbeebb/jason/.bash_history 1/1 4 KB 4. /media/jason/2253b90a-2608-4939-917d-85e1dbcbeebb/jason/.bash_logout 1/1 4 KB Total/best extents 4/4 Average size per extent 4 KB Fragmentation score 0 [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag] This device (/dev/sdb6) does not need defragmentation. Done. |
Then you may run this command to defragment your hard drive.
homer@homer-eME730:~$ sudo e4defrag /dev/sdb6 |
This does work very well, but you will not need to run this often.