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.
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:
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:
Here is another example that shows errors on a system:
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.