Safely Defragment an ext4 File System with e4defrag

A database file that keeps getting slower is often just badly fragmented, and e4defrag lets you measure that before touching anything. These examples use e4defrag from e2fsprogs 1.47.0, installed here as package version 1.47.0-2.4~exp1ubuntu4.1.

Budget about ten minutes for a file or small directory, plus however long the actual scan or move takes on your data. You need a shell and a target on ext4. Checking is read-only; defragmenting changes file placement and can eat real disk bandwidth.

1. Confirm the target is on ext4

Pick one exact file or directory rather than starting with an entire device, then check its file system type and mount point:

$ TARGET='/srv/data/example.db'
$ findmnt --target "$TARGET" --output TARGET,SOURCE,FSTYPE

The source and mount point are host-specific; the value that matters is ext4. Do not run this against a path on another file system: e4defrag is for files created on ext4 with extent support. Point it at a directory and it walks the files beneath it. Point it at an ext4 device and it resolves that to a mount point and walks the files there.

Checkpoint: confirm the path exists and is the object you meant to pick:

$ ls -ld -- "$TARGET"
$ df -hT -- "$TARGET"

Both checks are ordinary and unprivileged. Reach for sudo only if your account cannot read or traverse the target directory.

2. Measure before changing anything

Run -c against the narrowest useful target:

$ e4defrag -c "$TARGET"

Expect a fragmentation report followed by a success or failure result; the exact counts and wording depend on the installed program and your data. Treat the score as a guide, not a promise that defragmenting will speed anything up. Where the figures are available, the command also reports current and ideal extent counts and average extent size. The manual is explicit that -c never defragments the target, so this step is free to run as often as you like.

On this machine, pointing it at a sparse test file produced File has no blocks and a successful result. That is not a useful performance test: a file with no allocated blocks has nothing for the tool to improve. Measure real, allocated data instead.

3. Decide whether to proceed

$ df -hT -- "$TARGET"

Warning: there is no general undo command. The operation changes block placement, not file contents, but a power failure or unrelated storage problem is still an operational risk while it runs. Keep a current backup for data that matters, and schedule a large job for a maintenance window.

4. Defragment one file or directory

Once the measurement and the maintenance window justify it, run the command as the account that owns the data. This changes placement for one file:

$ e4defrag "$TARGET"
 

The installed syntax accepts the documented short options -c and -v. Use an unambiguous absolute path that does not start with a hyphen. The success text and counts vary with the file and the installed e2fsprogs build.

To process a directory instead, swap in a directory you have already checked:

$ e4defrag -v /srv/data/archive

-v prints errors and fragmentation counts before and after each file. Output varies with file size, allocation and permissions. It earns its keep on a small, targeted run; for a large tree, capture the output so you can review failures once the command finishes.

5. Verify the result

Measure the same target again. This does not change it:

$ e4defrag -c "$TARGET"

Compare the post-run report against the pre-run one. A lower score, or more contiguous extents, is evidence that placement changed. A similar score is also a valid result: free space or the file's existing layout may simply have limited what the tool could do.

Warning: do not infer success from a zero exit status alone. Check the report for errors and confirm the application still sees the expected file. If a directory run turns up failures, rerun -v on one affected file and inspect its ownership, permissions and file system. Files in lost+found, swap files and files allocated through indirect blocks are not supported by this tool.

6. Use elevated privileges only when required

Ordinary users can defragment their own files without help. The manual notes that the score is not printed for a non-root user when -c is used, so root earns you a complete diagnostic report, not permission to defragment something you otherwise could not touch:

$ sudo e4defrag -c "$TARGET"

Review the target again before you type a password. sudo does not make a non-ext4 target suitable, does not create free space, and does not provide an undo operation. Avoid running it over a whole mounted device when a file or directory does the job.

Done means