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 [-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.

SEE ALSO

e4defrag(8), dumpe2fs(8), debugfs(8), xfs_io(8)

Copied to clipboard