filefrag
Report file fragmentation
TLDR
Display a report for one or more files
Display a report using a 1024 byte blocksize
Display a report using a certain blocksize
Sync the file before requesting the mapping
Display mapping of extended attributes
Display a report with verbose information
SYNOPSIS
filefrag [-b size] [-e] [-k] [-v] [-w] [-x] [--] file...
PARAMETERS
-b size, --block-size=size
Use size as block unit (default: filesystem blocksize)
-e, --extent
Print extent info only, one line per extent, no summary
-k, --kilobytes
Display sizes in kilobytes instead of blocks
-v, --verbose
Enable verbose mode for more details
-w, --wide
Wide output format with all fields
-x, --extent-order
Include logical extent order information
--
End of options
DESCRIPTION
filefrag is a utility from the e2fsprogs package that analyzes file fragmentation on Linux filesystems, particularly ext2, ext3, and ext4. It displays the number of extents (or fragments on non-extent filesystems) used to store a file, helping diagnose fragmentation issues that can degrade performance.
For extent-based filesystems, it shows how many contiguous block ranges (extents) the file occupies. A low number of extents indicates good locality and less fragmentation. Typical output includes: filesystem type, file size, number of extents, and average extent size.
Example output for a fragmented file:
ext2fs fragmentation report for /path/to/file
average extent size = 1024 blocks / extent
/path/to/file: 15 extents found
It works by using ioctl calls like FIBMAP or FIEMAP to query the filesystem directly. Useful for database admins, sysadmins tuning storage, or verifying defragmentation tools like e4defrag. Supports multiple files and directories (recursive scan not built-in). Note: requires read permissions on the file and works best on unmounted or read-only filesystems for accuracy.
CAVEATS
Primarily for ext2/3/4; limited support for XFS/Btrfs. May report inaccurately on mounted filesystems with active writes. Requires read access; root often needed for full details. Not recursive by default.
SAMPLE OUTPUT
/tmp/test: 1 extent found ext2fs fragmentation report for /tmp/test average extent size = 4096 blocks / extent number of extents: 1 extent size total: 4096 blocks
FILESYSTEMS
Best on ext4 (extents); falls back to fragments on ext2/3. Partial XFS support via FIEMAP; inaccurate on non-Linux FS.
HISTORY
Developed as part of e2fsprogs by Theodore Ts'o and team since early 2000s. Pre-ext4 versions used fragment mapping; enhanced with FIEMAP support around 2008 for ext4 extents (Linux 2.6.28+). Widely used for performance tuning.


