The Linux ext2, ext3 and ext4 filesystems don’t need that much attention, but with time, after executing many many many reads/writes the filesystem may require optimization. Otherwise, the hard disk might become slower and may affect the entire system.
Unfortunately, the problem is not always solved with such a 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 run on HDDs and not on SSD. Defragging your SSD drive will only increase its read/write count and therefore shorten its life. Instead, if you are using SSD, you should use the TRIM function.
Let’s test if the system actually requires defragmentation. We can easily check this with a 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 a 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 an answer of "no" to all questions that may appear. This options basically allows using of e2fsck non-interactively.
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 attempt an 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 the following command.
$ sudo e4defrag or $ sudo e4defrag
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 /