Tools for defragging Linux
Tidy Up
You may not need to defrag your Linux system very often, but the tools are waiting when the need arises.
Everyone knows that defragmenting a filesystem on Linux is unnecessary. However, like much of what everyone knows, that idea is only partly correct. Why else would Linux include utilities such as e2freefrag
, filefrag
, e4defrag
, and fsck
to allow you to check fragmentation and fix it?
Defragmenting is a means of improving how files are stored on a hard drive. In a filesystem like FAT32, files are written as close to the start of the disk as possible for quick access. However, when existing files are edited, this tactic often means there is no room to store the changes in the same space as the original file. Instead, the changes have to be stored someplace else, which causes defragmentation and can slow the system.
By contrast, Linux's ext2, ext3, and ext4 filesystems write new files with space between them. When a file is edited, the drive usually has space to store all the contents together, using space immediately after the original file. As a result, fragmentation is minimal under many conditions, so defragging does little to improve performance. In fact, if you have a true solid state drive – as opposed to the hybrid drives often sold as solid state – defragmenting becomes an obsolete concern on any operating system.
However, traditional hard drives or hybrid drives remain common, if only because they are cheaper, so fragmentation can still be a concern. For example, when an ext4 filesystem is more than 80 percent full, a lack of drive space causes fragmentation to increase. Similarly, FAT32 and other older filesystems may still be in use on dual-boot systems or on external drives. Sometimes, too, large networks may have too much activity for the filesystem to allocate space intelligently. In fact, even a home system may benefit from occasional defragmenting every few months or so.
Note that on Windows, defragging usually deals with entire partitions. By contrast, although Linux can deal with partitions, it can also work on individual files and directories. This ability means that in some cases you can observe and correct defragmentation without having to dismount a drive or boot from a Live CD.
Working with Free Space and Files
Probably the simplest tool for working with fragmentation is e2freefrag. Despite its name, e2freefrag examines free space on not just ext2 filesystems, but also ext3 and ext4 filesystems. The distribution of free space matters because, the more fragmented a partition is, the more files will fragment when they are written (Figure 1). Possibly, if you deal in the massive files sometimes found in graphics or multimedia work, you might not want to write to a badly fragmented partition knowing that your file will be noncontiguous from the start and become even slower than it already is.
In such cases, enter:
e2freefrag /dev/PARTITION
If you want to check for a particular size, add the option -c CHUNK
, with the chunk entered in kilobytes.
Similarly, to check the fragmentation of directories or files, use filefrag (Figure 2) with the following format:
filefrag -v FILEPATH
The e4defrag tool gives an even more detailed report. Although it does not work on ext2, ext3, or swap partitions or files in the lost+found directory, e4defrag has the advantage of giving a score to the files or directory it reports on. A score of 0-30 means no fragmentation exists, and a score of 56 or higher means that fragmentation is serious enough that e4defrag automatically defragments it (Figure 3).
To run e4defrag, enter:
e4defrag -cv <Directory or File>
If you specify a directory, then all files in it are checked and defragmented as necessary. A score is only given when e4defrag is run as root, but regular users can run e4defrag on their own files and directories.
Defragging
Linux periodically has third-party defraggers available. Most of these defraggers come and go, but one currently worth a look is ShAkE [1].
The documentation and web pages hint that ShAkE's developers are skeptical about the need for defraggers, at least on Linux, because the utility is also available for Windows. However, ShAkE does have a number of useful options, not least of which is the ability to defrag multiple filesystems with a single command using the --many-fs
and --max-device
options. For cautious users, it also offers --pretend
, which does a test run without actually making changes.
However, those who are cautious might prefer to use fsck, the standard utility for repairing partitions. You may have seen it running during bootup, where it has a staggered schedule so that the bootup process is not delayed by several partitions being checked at the same time.
When fsck is run during bootup, any necessary defragmentation is automatically performed. However, you can also run fsck yourself. Use fdisk -l
to view a list of partitions. Then, assuming you are using ext4 filesystems – the default on most modern systems – start from a Live CD so that the hard drive systems are unmounted and enter:
fsck.ext4 -fv /dev/PARTITION
Or, for a report specifically on fragmentation, you can do:
fsck.ext4 -fv -E fragcheck /dev/PARTITION
Both of these commands force a verbose report on the partition specified. Look in the output for the ratios of noncontiguous to contiguous (fragmented to unfragmented) files and drives. If the noncontiguous files and drives outnumber the contiguous ones, then the partition will almost certainly benefit from defragging. However, you might want to defragment before that point to see if any improvements result.
If you decide to defrag, adding the -p
option to fsck defragments the partition. You will need to run fsck.ext4 -fv /dev/PARTITION
on all the partitions on your system. If your system either regularly suffers from fragmentation or has very little, you might run
tune2fs -c MAX-MOUNT-COUNT /dev/DEVICE
to change how often a partition is checked. However, remember to stagger the checks for all the partitions to minimize the delay as fsck runs.
Beyond the Myth
Misinformation about defragging on Linux is widespread. Many believe it is unnecessary, making it one of the main reasons for adopting Linux. For casual users who switch computers every year or so, this point may be valid. Such users may be working on a new computer long before defragmentation matters on their system.
An indication of how efficient Linux filesystems can be may be seen in the fact that /var/log
and /var/spool/cups
– two of the most active directories on my home system – had scores of 3 and 7, respectively, from e2defrag after three years of daily use. This is one-eighth the score needed to require defragging.
This resilience of Linux is worth knowing. Windows may hide the need for defragging by running a defragmenter in the background, but Linux copes primarily by being resistant to the need for defragging in many cases. However, if you are managing a system with heavy traffic or simply want to squeeze the maximum performance from your system, it is reassuring that the defragging tools are there if you need them.
Infos
- ShAkE: http://vleu.net/shake/
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.