How to Repair and Defragment Linux System Partitions and Directories

People who use Linux often think that it doesn’t require defragmentation. This is a common misunderstanding across Linux users. Actually, the Linux operating system does support defragmentation. The point of the defragmentation is to improve I/O operations like allowing local videos to load faster or extracting archives significantly faster.

Defragment Linux System Partitions
Defragment Linux System Partitions and Directories

The Linux ext2, ext3 and ext4 filesystems don’t need that much attention, but with time, after executing many many many read/writes the filesystem may require optimization. Otherwise the hard disk might become slower and may affect the entire system.

In this tutorial I am going to show you few different techniques to perform defragmentation on files. Before we start, we should mention what the common filesystems like ext2,3,4 do to prevent fragmentation. These filesystems include technique to prevent the effect. For example filesystems reserve free block groups on the hard disk to store growing files completely.

Unfortunately the problem is not always solved with such mechanism. While other operating systems may require expensive additional software to resolve such issues, Linux has some easy to install tools that can help you resolve such problems.

How to Check a Filesystem Requires Defragmentation?

Before we start I would like to point that the operations below should only be ran on HDDs and not on SSD. Defragging your SSD drive will only increase its read/write count and therefore shorten it’s life. Instead, if you are using SSD, you should use the TRIM function, which is not covered in this tutorial.

let’s test if the system actually requires defragmentation. We can easily check this with tool such as e2fsck. Before you use this tool on a partition on your system, it is recommended to unmount that partition with. This is not completely necessary, but it’s the safe way to go:

$ sudo umount <device file>

In my case I have /dev/sda1 mounted at /tmp:

Disk Partition Table Before
Disk Partition Table Before

Keep in mind that in your case the partition table might be different so make sure to unmount the right partition. To unmount that partition you can use:

$ sudo umount /dev/sda1

Now let’s check if this partition requires defragmentation, with e2fsck. You will need to run the following command:

$ sudo e2fsck -fn /dev/sda1

The above command will perform a file system check. The -f option forces the check, even if the system seems clean. The -n option is used to open the filesystem in read-only and assume answer of "no" to all questions that may appear.

This options basically allows to use e2fsck non-interactively. If everything is Okay, you should see result similar to the one shown on the screenshot below:

e2fsck Healthy Partition
e2fsck Healthy Partition

Here is another example that shows errors on a system:

e2fsck With Errors
e2fsck With Errors

How to Repair Linux Filesystem Using e2fsck

If errors appear, you can attempt a repair of the filesystem with e2fsck with the “-p” option. Note that in order to run the command below, the partition will need to be unmounted:

$ sudo e2fsck -p <device file>

The “-p” options attempts automatic repair on the file system for problems that can be safely fixed without human intervention. If a problem is discovered that may require the system administrator to take additional corrective action, e2fsck will print a description of the problem and will exit with code 4, which means “File system errors left uncorrected”. Depending on the issue that has been found, different actions might be required.

If the issue appears on a partition that cannot be unmounted, you can use another tool called e4defrag. It comes pre-installed on many Linux distros, but if you don’t have it on yours, you can install it with:

$ sudo apt-get install e2fsprogs         [On Debian and Derivatives]
# yum install e2fsprogs                  [On CentOS based systems]
# dnf install e2fsprogs                  [On Fedora 22+ versions] 

How to Defragment Linux Partitions

Now it’s time to defragment Linux partitions using following command.

$ sudo e4defrag <location>
or
$ sudo e4defrag <device>

How to Defragment Linux Directory

For example, if you wish to defragment a single directory or device, you can use:

$ sudo e4defrag /home/user/directory/
# sudo e4defrag /dev/sda5

How to Defragment All Linux Partitions

If you prefer to defragment your entire system, the safe way of doing this is:

$ sudo e4defrag /

Keep in mind that this process may take some time to be completed.

Conclusion

Defragmentation is an operation that you will rarely need to run in Linux. It’s meant for power users who know what exactly they are doing and is not recommended for Linux newbies. The point of the whole action is to have your filesystem optimized so that new read/write operations are performed more efficiently.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Marin Todorov
I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

9 thoughts on “How to Repair and Defragment Linux System Partitions and Directories”

  1. Even though the filesystem prevents defragmenting. HDD operation is slowing down due to the dispersion of data, you still need to defragment regularly not for fragmentation but to bring files back in sequential order.

    Reply
  2. Conclusion : you are assuming all users on the internet reading this, are using “ext4” filesystem, ignoring for complete, exist another like : ext2, ext3, reiserfs, reiser4, jfs, xfs, zfs, btrfs, etc. and for each filesystem the def rag its different.

    And in the title should be something like “How to Repair and Defragment Linux ext4 Partitions

    Reply
  3. Erm you say you would check with e2fsck If it is necessary to defrag the partition but then don’t do that and instead just check/repair the filesystem and run a program that is called e4defrag.
    You do not say what it is doing.

    Reply
  4. Two small points … nice typo in the unmount command, I think it supposed to be umount. Also, you forgot to mention that you need to re-mount the partition before running e4defrag

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.