← All commands
THE COMMAND REFERENCE

sum

Print an old-style checksum and block count of files.

Last updated

BusyBox

Implementation-level reference; examples are not certified for every installed version.

Overview

sum prints a 16-bit checksum of a file and the number of 1024-byte blocks it takes. It is an old command, kept for compatibility. For new uses, cksum or sha256sum are better, because a 16-bit sum misses many changes.

Background

Today the name covers GNU sum, the sum that macOS ships, and the sum applet in BusyBox. The macOS manual has no dated history for it. All three use the BSD checksum algorithm by default, so they agree on the number.

Examples

Print the checksum and block count for some text:

printf abc | sum

Print them for a file; awk keeps just the two numbers, because the file name is not printed on every system:

printf abc > f.txt && sum f.txt | awk '{print $1, $2}'

Compare two files by checksum:

printf abc > a.txt && printf abc > b.txt && test "$(sum a.txt | cut -d' ' -f1)" = "$(sum b.txt | cut -d' ' -f1)" && echo same

Common mistakes

A 16-bit checksum has only 65,536 values, so two different files will often match by chance; do not use sum to prove files are equal. The spacing of the output is not the same on all systems, so split on whitespace. The block count is rounded up, so a 3-byte file counts as 1.

Portability

Only the default BSD algorithm is in the Common examples. GNU also has -s for the System V algorithm, which gives different numbers. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

About BusyBox sum

BusyBox bundles sum as an applet, and anything beyond the common options depends on how it was built. This page's examples ran on BusyBox 1.37.0.

BusyBox notes

BusyBox sum prints the same two numbers. In the 1.37.0 check it did not print the file name after them for a single file, which GNU and macOS do.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.