LinuxCommandLibrary

filefrag

Report file fragmentation

TLDR

Display a report for one or more files

$ filefrag [path/to/file1 path/to/file2 ...]
copy

Display a report using a 1024 byte blocksize
$ filefrag -k [path/to/file]
copy

Display a report using a certain blocksize
$ filefrag -b[1024|1K|1M|1G|...] [path/to/file]
copy

Sync the file before requesting the mapping
$ filefrag -s [path/to/file1 path/to/file2 ...]
copy

Display mapping of extended attributes
$ filefrag -x [path/to/file1 path/to/file2 ...]
copy

Display a report with verbose information
$ filefrag -v [path/to/file1 path/to/file2 ...]
copy

SYNOPSIS

filefrag [options] files...

PARAMETERS

-v
    
Verbose output. Displays detailed information about each extent, including the logical block number, physical block number, and length.

-s
    
Synchronous (safe) mode. Before checking a file, ensures all pending writes to the file are flushed to disk. This can slow down the operation but ensures accurate reporting for actively written files.

-x
    
Display raw extent information. Shows the extent map of the file in a more technical, raw format, useful for debugging or detailed analysis.

-h
    
Display help message. Shows a brief usage summary and available options.

-V
    
Display version information. Shows the version of filefrag.

DESCRIPTION

filefrag is a utility for reporting the number of extents (contiguous blocks) a file occupies on a disk. In file systems that support extents (like ext4, XFS, Btrfs), a single extent can represent a large contiguous block of data. When a file is highly fragmented, it is stored in many non-contiguous blocks, leading to a high number of extents. This can negatively impact read/write performance.
filefrag helps identify such files, allowing administrators or users to assess fragmentation levels. A lower extent count indicates better contiguity and less fragmentation, improving I/O efficiency. It's often used by system administrators to diagnose performance issues related to disk fragmentation, though modern journaling filesystems like ext4 mitigate fragmentation far better than older filesystems.

CAVEATS

filefrag primarily works with filesystems that support extents (e.g., ext4, XFS, Btrfs). For older filesystems or those not supporting extents, its output might be less meaningful or different.
It only reports on fragmentation; it does not defragment files.
Modern Linux filesystems (like ext4) are designed to minimize fragmentation automatically, so severe fragmentation requiring filefrag is less common than in the past.

EXTENT-BASED ALLOCATION

filefrag's output is based on the concept of extents. An extent is a contiguous block of disk space allocated to a file. Instead of tracking individual data blocks, filesystems using extents track larger contiguous regions. This significantly reduces the metadata overhead and improves performance, especially for large files, as fewer entries are needed to describe the file's layout on disk. A file with one extent is perfectly contiguous. A file with multiple extents is fragmented.

HISTORY

filefrag is part of the e2fsprogs package, a suite of utilities for the ext2, ext3, and ext4 filesystems. Its development is tied to the evolution of these filesystems, particularly the introduction and optimization of extent-based allocation to improve performance and reduce fragmentation on large files.

SEE ALSO

e2fsck(8), tune2fs(8), df(1), du(1), stat(1)

Copied to clipboard