LinuxCommandLibrary

compsize

Determine the compression ratio of filesystems

TLDR

Calculate the current compression ratio for a file or directory

$ sudo compsize [path/to/file_or_directory]
copy

Don't traverse filesystem boundaries
$ sudo compsize [[-x|--one-file-system]] [path/to/file_or_directory]
copy

Show raw byte counts instead of human-readable sizes
$ sudo compsize [[-b|--bytes]] [path/to/file_or_directory]
copy

SYNOPSIS

compsize [OPTIONS...] [FILE...]

PARAMETERS

-h, --help
    Print brief usage information and exit.

-V, --version
    Print command version and exit.

-r, --recursive
    Recurse into subdirectories, aggregating totals.

-a, --all
    Include files not compressed by filesystem.

--inodes
    Show detailed inode compression information.

--raw-json[=FILE]
    Output raw data as JSON to FILE or stdout.

DESCRIPTION

compsize is a utility from the systemd project designed to analyze compression efficiency on Btrfs and ZFS filesystems. It examines specified files or directories, reporting the uncompressed logical size, the actual compressed size stored on disk, and the space savings achieved through filesystem-level compression.

Typical output is a formatted table with columns for Path, Logical (uncompressed size), Compressed (on-disk size), Saved (difference), and Ratio (compression ratio as percentage). For directories with -r, it aggregates totals and lists subfiles. The --all option includes uncompressed files for complete disk usage views, while --inodes adds inode-level details.

JSON output via --raw-json enables scripting and parsing. Ideal for system administrators monitoring compression effectiveness, identifying incompressible data (e.g., already compressed media), or optimizing storage. It leverages filesystem ioctls to query metadata without decompressing data, making it efficient. Supported compression algorithms include zstd, lzo, zlib on Btrfs/ZFS. Note: ineffective on non-compressed filesystems like ext4.

CAVEATS

Only works on Btrfs or ZFS with compression enabled; fails on other filesystems. May require read access to filesystem metadata. Recursive scans on large directories can be slow.

SAMPLE OUTPUT

For a file:
Path Logical Compressed Saved Ratio
file 1.0M 300K 700K 70.0%
Aggregates show directory totals.

USE CASES

Verify zstd compression savings; find low-ratio files for recompression or relocation; integrate with monitoring scripts via JSON.

HISTORY

Introduced in systemd 239 (December 2018) to aid Btrfs/ZFS users; enhanced in later versions with JSON output and inode support.

SEE ALSO

btrfs(8), zfs(8), du(1), filefrag(1)

Copied to clipboard