Tools for defragging Linux
Tidy Up
![](/var/linux_magazin/storage/images/issues/2015/178/command-line-defragging-linux/78002314_20.png/655153-1-eng-US/78002314_20.png_medium.png)
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.
![](/var/linux_magazin/storage/images/issues/2015/178/command-line-defragging-linux/figure-1/655156-1-eng-US/Figure-1_large.png)
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
![](/var/linux_magazin/storage/images/issues/2015/178/command-line-defragging-linux/figure-2/655159-1-eng-US/Figure-2_large.png)
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.