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
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.