1. Home
  2. Linux

How To Defragment A Hard Drive On Linux

There are a lot of misconceptions out there about file systems and operating systems. The most perpetuated misconception is that only Windows hard drives on the NTFS file system can ever get fragmented. This is a myth. Fragmentation isn’t a bad invention by Microsoft. In fact, fragmentation of files can happen on most file systems, unless they’re specifically designed NOT to be fragmented (which is pretty rare). Linux users are not immune from going through the defragmentation process. Sadly popular websites online often perpetuate the notion that “Linux is immune”. In part, this is half-true. Linux file systems DO get file system fragmentation at a slower rate than something like Microsoft’s NTFS, but they aren’t immune. In this article, we’ll go over what to do when your Linux PC reaches critical levels of fragmentation and how to defragment a hard drive on Linux.

Creating A Live Linux Disk

Unlike on Windows, Linux file systems shouldn’t be defragged while in use. This means if you ever want to defragment a hard drive on Linux, particularly one that houses your Linux installation, a live disk is preferable. When it comes to a live disk, any old Linux will do (as long as there is root terminal access). That being said, in this guide we’ll be recommending that you use an Arch Linux live disk.

Arch is best because it loads right to a root shell. This makes it the perfect operating system for maintenance work. To make the live disk, download the latest ISO file from the Arch website, and get the Etcher USB ISO writing tool. Making the live disk is easy, and the Etcher tool will walk you through the entire process.

Prep Your System

The defrag can run without going through this process, but it is not advisable. Things can go wrong, and even fail to complete if there are any bad blocks present on any of the partitions.

In the Arch Linux prompt, use lsblk. This command lists all block devices (data drives and etc) currently connected to the computer. Using the list that lsblk shows, determine which hard drives (and partitions) you want to run a defrag on, and keep in mind which labels are what.

Next, go to the Arch shell prompt, and type:

fsck /dev/sdXY -y

Note: replace X and Y with your hard drive’s label (as it says in lsblk). 

The fsck tool will scan any hard drive partition for bad blocks, corrupted blocks and garbage data. It will clean everything out of the partitions, so that the defrag can run smoothly.

Depending on how many bad bits of data are on the partition that the fsck command is running on, the cleaning process could take quite a while. Do not worry; this process is entirely automatic. There is no need to do anything, thanks to the use of the -y switch which will auto-answer yes to every question the program asks.

Detecting Fragmentation

To detect fragmentation, we’ll need to again make use of fsck. This time, run it with the -fn switch:

fcsk -fn /dev/sdXY

The -fn flag tells the fsck tool to quickly check the filesystem. It determines if the entire system is “contiguous” (aka fragmented in any way). The lower the number the better. It’s best to only run the defrag if your filesystem is 15%+ fragmented.


The Arch Linux comes with a powerful defrag tool right out of the box, with no need to install anything. This tool is called e4defrag. It is very light, but very powerful. Run it on any Linux partition, and it’ll analyze, and defragment a hard drive on Linux pretty easily. To use it, find the name of the partition you’d like to clean up with the lsblk command.

mount /dev/sdXY /mnt

Then, start the defragmentation process:

e4defrag /dev/sdXY

Note: While this tutorial states plainly that users shouldn’t run the e4defrag without using a live disk, that doesn’t mean that it is impossible to do so on a running system. We do not recommend doing this due to the fact that it’s impossible to fix and clean bad-blocks while a hard drive is in use by Linux. This handicaps the process leading up to defragmentation.

If you wish to skip cleaning your drive of bad blocks and garbage data before using e4defrag, it is possible to do this process on any Linux distribution. Most Linux distributions ship the e4defrag tool by default.

If for some reason, e4defrag is not on your Linux PC, it’s very easy to find. Simply search the package manager you normally use to install programs with for “e4defrag”, and install it.


Fragmentation is a reality of computing. File systems get more and more cluttered as time goes on. While it’s true that on Linux, this problem only really presents itself when a drive is almost full, it’s still a problem. That’s why it’s so concerning that this myth that “Linux doesn’t require fragmentation” isn’t helpful. Hopefully, with the help of this tutorial, you’ll realize that it’s up to you to keep your Linux hard drive in tip top shape!

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.