I know everybody says EXT3 and EXT4 filesystems don’t need defragmentation and in most cases, that’s true! But sometimes they actually do need defragmentation. There is an entire discussion rather or not EXT3 needs defragmentation but I’ll not get into that. It just depends on the way a filesystem is used, when it was last formated and how full it is. So don’t take my word for it, and go take a look at the fragmentation level (will tell you how to do that in just a moment) of your hard drive.
I’ve recently scanned my drives and found 1 partition (which hasn’t been formatted in a very very long time) having a fragmentation level of over 70%. Yes, I was amazed myself, but it seems this is possible! That must be because I use that partition for BitTorrent downloads so I constantly download new files, delete others, etc. BitTorrent clients constantly read and write files which is quite bad from the fragmentation point of view. For instance, my home partition has a fragmentation level of under 1%, but I never download torrents there.
How to check the fragmentation level in Linux
Then run fsck in a terminal:
fsck -nvf /dev/hda1
replace “hda1” with your partition.
You will then see something like this:
fsck from util-linux-ng 2.16 e2fsck 1.41.9 (22-Aug-2009) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information 1955 inodes used (0.08%) 1554 non-contiguous files (79.5%) 0 non-contiguous directories (0.0%) # of inodes with ind/dind/tind blocks: 1660/1224/0 3706004 blocks used (72.40%) 0 bad blocks 1 large file 1825 regular files 121 directories 0 character device files 0 block device files 0 fifos 0 links 0 symbolic links (0 fast symbolic links) 0 sockets -------- 1946 files
Notice the “non-contiguous” part – that’s the fragmentation level for that partition (smaller is better).
Defragmenting the Linux partitions
1. Shake is a defragmenter that runs in userspace, without the need of patching the kernel and while the system is used (for now, on GNU/Linux only).
It works by rewriting fragmented files. But it has some heuristics that could make it more efficient than other tools, including defrag and, maybe, xfs_fsr.
As an example, it allows you to write find -iname ‘*.mp3’ | sort | shake to defrag all mp3 in a directory, putting together on the disk those close in lexical order.
In Ubuntu, you can add Shake PPA.
Alternatively, you can simply download Shake .deb files:
For other Linux distributions: There is an installer that you can run as a shell script.
Running the actual defragmentation using Shake
Again: “my_folder” can be the whole partition or just a folder, but not the partition as in /dev/sda1, but the folder where it is mounted, as in /media/disk1.
Now go do something useful until this Shake finishes it’s job 🙂
Then, check again the fragmentation level.
For more info on Shake, visit it’s HomePage.
Defragmenting using Defrag
Using Defrag is very easy: copy the script in a folder you want to defragment. This can be just a normal folder or the top level directory of a partition.
To run it, open a terminal, navigate to the folder where defrag was copied (this also being the folder you want to defragment), and type:
P.s.: You need to run it on a mounted partition.
The maximum benefit seems to occur with two runs, and it seems to be effective only if the initial fragmentation is > 2.5%.