I get this question a lot. And generally speaking the answer is a resounding "no". I have gone nearly twelve years using Linux without defragging a drive and I've never noticed a slowdown on a system. But just because you don't need to doesn't mean you can't. I have found it possible to actually check the fragmentation of a linux mount point and defragment that mount point.
In this article I will discuss this very issue and then I will show you how you can test the fragmentation of a mount point on your Linux drive and then defragment that mount point.
What it isn't necessary
This is the question I get nearly every time I tell a user that it is not necessary to defrag a Linux drive. The first and foremost reason you do not have to defrag a system is that the majority of files on a system need super-user permission to move. Oh sure you can move anything you want around in your ~/ directory. But try to move anything in /usr/bin, /opt, /sbin or any other directory outside of ~/ (without super user permission) and see how far you get. What this means is that during general, every-day usage the vast majority of files are not being moved around on your system. The only files you really need concern yourself with are the ones in your home directory - and those files have little to nothing to do with the performance of your machine.
Another difference is that some other operating systems try their best to place files as close the front of the drive as they can - without gaps. When files get moved around, these gaps appear, making it hard for the drive to be read. The Linux operating system does not do this. Instead the system starts in the center and places files not in a random fashion, but doesn't concern itself with placing files next to one another in the start of the drive. So when spaces are created it's not a big deal because the system is used to those spaces. The only time you will notice defragmentation on a Linux drive is when the drive is over 95% full. At that point the seeming "randomness" of placement will have caught up and the spaces between files might not allow for the addition of more files.
So when this happens...is it possible to defragment? Yes it is.
I discovered a very handy Perl script that will allow you to check a mount point for fragmentation. The script can be found here in the discussion. Copy that code into a file called fragmentation.pland give that file executable permissions with the command chmod u+x fragmentation.pl. Now issue the command:
sudo ./fragmentation.pl /home/USER
Where USER is the user who's home directory you want to check.
You will most likely be surprised at how how low the number is. It will give you a report like:
1.30108895488615% non contiguous files, 1.01067741479282 average fragments.
Now, say you do want to defragment that home directory. You can do so with this handy piece of code. Save that piece of code in a file called defrag.pland give it executable permissions with the command chmod u+x defrag.pl. Now issue the command:
sudo ./defrag.pl /home/USER
Where USER is the user who's home directory you want to defragment.
Now this task can take some time, depending upon the size of your ~/ and how much is in that directory. But once it is done, issue the fragmentation.pl command again and I bet you will find the results positive.
Although you will most likely never have to run this, it is nice to know that it is possible. The Linux system rarely gets fragmented to the point you will ever notice the slightest hiccup...at least not until that drive is nearly full. And considering the cost to size ratio of today's drives, the possibility of them filling up is slim. And if they do, you'll probably just go out and buy another drive.
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.