LinuxCommandLibrary

pretty-bytes

Convert bytes to human-readable string

TLDR

Convert numeric bytes value to a human-readable string

$ pretty-bytes [1337]
copy

Convert numeric bytes value from stdin to a human-readable string
$ echo [1337] | pretty-bytes
copy

Display help
$ pretty-bytes --help
copy

SYNOPSIS

pretty-bytes [OPTIONS] NUMBER_OF_BYTES
COMMAND | pretty-bytes [OPTIONS]

PARAMETERS

-p, --precision <num>
    Specifies the number of decimal places to include in the output. For example, --precision 2 would display 1.23 MB.

-s, --si
    Uses SI units (powers of 1000, e.g., KB, MB) instead of IEC units (powers of 1024, e.g., KiB, MiB). By default, many implementations use IEC units.

-f, --force-unit <unit>
    Forces the output to be in a specific unit (e.g., KB, MB, GB), regardless of the automatic determination. This can be useful for consistent display across multiple values.

-0, --no-decimals-if-whole
    Omits decimal places if the value is a whole number (e.g., outputs 2 MB instead of 2.00 MB).

-c, --compact
    Uses a more compact notation, often omitting spaces (e.g., 1MB instead of 1 MB) or using shorter unit names (e.g., 1M).

-h, --help
    Displays a help message and exits.

-v, --version
    Shows the command's version information and exits.

DESCRIPTION

The pretty-bytes command, often implemented as a standalone script or utility, is designed to transform a raw number of bytes into a more human-readable format. Instead of displaying a large, raw number like 1048576, it intelligently converts it to a more intuitive representation such as 1 MB or 1.00 MiB. This conversion makes byte sizes much easier to comprehend quickly, which is crucial when dealing with file sizes, disk usage reports, or network transfer speeds where large byte counts would otherwise be cumbersome to interpret. It automatically determines the most appropriate unit (bytes, KB, MB, GB, TB, PB, etc.) to represent the given size, and typically offers options to control precision and unit systems (binary IEC vs. decimal SI prefixes).

CAVEATS

The pretty-bytes command is not a standard, pre-installed utility universally found on all Linux distributions. Its functionality is commonly provided by programming libraries (like Node.js's pretty-bytes) or can be found in other command-line tools such as numfmt. If you intend to use a specific `pretty-bytes` utility, you might need to install a particular package, use a custom script, or compile it from source. Implementations may vary in available options, default behaviors, and the exact set of supported units.

USAGE EXAMPLES

To demonstrate its common utility:

pretty-bytes 123456789
# Output: 123.46 MB

pretty-bytes --si 1000000000
# Output: 1.00 GB

echo 2048 | pretty-bytes --force-unit KB
# Output: 2.00 KB

du -b my_file.txt | cut -f1 | pretty-bytes
# Output: (e.g., 5.34 KB)

UNIT SYSTEMS

There are two primary systems for byte prefixes that pretty-bytes often differentiates:
1. Binary (IEC): Uses 1024 as the base (2^10). Units are KiB (kibibyte), MiB (mebibyte), GiB (gibibyte), TiB (tebibyte). This system is commonly used for measuring RAM, file sizes, and disk space by operating systems.
2. Decimal (SI): Uses 1000 as the base (10^3). Units are KB (kilobyte), MB (megabyte), GB (gigabyte), TB (terabyte). This system is typically used by hard drive manufacturers for advertised capacities and for network speeds (e.g., Mbps).

SEE ALSO

numfmt(1), du(1), df(1), ls(1), find(1)

Copied to clipboard