How to Display Human-Readable File Sizes in Rsync Output Using Format Specifiers


2 views

When working with rsync's --out-format option, the default byte-size output isn't always the most convenient for human operators. The man pages mention a solution but don't provide clear implementation examples.

The key lies in rsync's numerical escape sequences with apostrophe modifiers:

%b    - raw bytes (default)
%'b   - human-readable with one unit (KB/MB/GB)
%''b  - human-readable with decimal
%'''b - human-readable with SI units

Here's the correct syntax for different readability levels:

# Basic human-readable
rsync -avzh --stats --out-format="%t %f %'b"

# With decimal precision
rsync -avzh --stats --out-format="%t %f %''b"

# SI units style
rsync -avzh --stats --out-format="%t %f %'''b"

The output you saw (%'b appearing literally) typically happens when:

  • Using older rsync versions (pre-3.0.0)
  • Improper escaping in shell scripts
  • Copy-paste artifacts from HTML sources

Here's a complete command that works reliably:

rsync -avzh --stats --out-format="[%t] %f (%''b)" \
--progress /source/path/ user@remote:/dest/path/

Sample output:

[2023/11/15 14:30:45] project/assets/video.mp4 (1.23GB)
[2023/11/15 14:30:47] project/docs/manual.pdf (4.56MB)

Combine multiple format specifiers for comprehensive output:

rsync -avzh --out-format="%t %-50n %''l %''b %c %u %g %f"

This shows:

  • Timestamp
  • Filename (left-aligned, 50 chars)
  • Human-readable length
  • Human-readable bytes
  • Checksum
  • User/group
  • Full path

Note that human-readable formatting works best in rsync 3.x+. For older versions, consider piping through numfmt:

rsync -avz --out-format="%b" | numfmt --to=si

If formats aren't working:

  1. Check rsync version with rsync --version
  2. Test format strings in simple commands first
  3. Try different quote styles (single vs double)
  4. Verify shell escaping in scripts

When working with rsync's --out-format option, many developers encounter the same frustration - file sizes displayed in raw bytes. While the rsync manual mentions human-readable formatting through apostrophes, the actual implementation isn't always straightforward.

The rsync manual specifies three levels of human-readable formatting, controlled by apostrophes:

%b    - Bytes (default)
%'b   - Base-2 human readable (KiB, MiB)
%''b  - Base-10 human readable (KB, MB)
%'''b - Auto-scaling between base-2 and base-10

Here's how to properly format the command for human-readable output:

# Basic human-readable (KiB, MiB)
rsync -avzh --stats --out-format="%t %f %'b"

# Base-10 human-readable (KB, MB)
rsync -avzh --stats --out-format="%t %f %''b"

# Auto-scaling format
rsync -avzh --stats --out-format="%t %f %'''b"

The issue you encountered with %'b appearing literally in the output typically occurs when:

  • Using the wrong quote types (mixing single and double quotes incorrectly)
  • Shell interpretation issues (especially in bash scripts)
  • Terminal character encoding problems

Here's a bulletproof version that works in most environments:

rsync -avzh --stats --out-format=$'%t %f %\'b'

For comprehensive transfer logging:

rsync -avzh --stats --out-format=$'[%Y-%m-%d %H:%M:%S] %f (%\'b) [%o/%u]'

This will produce output like:
[2023-11-15 14:30:22] data/backup.zip (1.4M) [pi/pi]

If you're still seeing literal %'b in output, try these alternatives:

# Use printf-style formatting
rsync -avzh --stats --out-format="$(printf "%t %f %\'b")"

# Or pre-define the format string
FORMAT_STRING=$'%t %f %\'b'
rsync -avzh --stats --out-format="$FORMAT_STRING"