# Sum (Unix)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Sum_(Unix)
> Markdown URL: https://mediated.wiki/source/Sum_(Unix).md
> Source: https://en.wikipedia.org/wiki/Sum_(Unix)
> Source revision: 1132116168
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Unix command}}
{{lowercase title}}
{{Infobox software
| name                   = sum
| logo                   = 
| screenshot             = 
| screenshot size        = 
| caption                = 
| author                 = [Ken Thompson](/source/Ken_Thompson)
| developer              = [AT&T Bell Laboratories](/source/AT%26T_Bell_Laboratories)
| released               = {{Start date and age|1971|11|3}}
| latest release version = 
| latest release date    = 
| operating system       = [Unix](/source/Unix), [Unix-like](/source/Unix-like), [Inferno](/source/Inferno_(operating_system))
| platform               = [Cross-platform](/source/Cross-platform)
| genre                  = [Command](/source/Command_(computing))
| license                = [coreutils](/source/coreutils): [GPLv3+](/source/GPLv3%2B)
| website                = 
}}
'''{{mono|sum}}''' is a legacy utility available on some [Unix](/source/Unix) and [Unix-like](/source/Unix-like) [operating system](/source/operating_system)s. This utility outputs a 16-bit [checksum](/source/checksum) of each argument [file](/source/computer_file), as well as the number of [blocks](/source/Block_(data_storage)) they take on disk.<ref name="sum-invocation">{{man/format|1|sum|https://www.gnu.org/software/coreutils/manual/html_node/sum-invocation.html}} — manual pages from [GNU](/source/GNU) [coreutils](/source/coreutils)</ref> Two different checksum algorithms are in use. POSIX abandoned <code>sum</code> in favor of [cksum](/source/cksum).

==Overview==
The {{mono|sum}} program is generally only useful for historical interest. It is not part of POSIX. Two algorithms are typically available: a [BSD checksum](/source/BSD_checksum) and a [SYSV checksum](/source/SYSV_checksum). Both are weaker than the already weak 32-bit CRC used by [cksum](/source/cksum).<ref name=fbsd>{{man|1|sum|FreeBSD}}</ref>

The [default](/source/Default_(computer_science)) algorithm on FreeBSD and GNU implementations is the BSD checksum. Switching between the two algorithms is done via command line options.<ref name=fbsd/><ref name="sum-invocation"/>

The two commonly used algorithms are as follows.

The BSD sum, -r in GNU sum and -o1 in FreeBSD cksum:
* Initialize checksum to 0
* For each byte of the input stream
** Perform 16-bit bitwise right rotation by 1 bit on the checksum
** Add the byte to the checksum, and apply modulo 2 ^ 16 to the result, thereby keeping it within 16 bits
* The result is a 16-bit checksum
The above algorithm appeared in [Seventh Edition Unix](/source/Seventh_Edition_Unix).

The System V sum, -s in GNU sum and -o2 in FreeBSD cksum:
* checksum0 = sum of all bytes of the input stream modulo 2 ^ 32
* checksum1 = checksum0 modulo 2 ^ 16 + checksum0 / 2 ^ 16
* checksum = checksum1 modulo 2 ^16 + checksum1 / 2 ^ 16
* The result is a 16-bit checksum calculated from the initial 32-bit plain byte sum

==Syntax==
The {{mono|sum}} utility is invoked from the [command line](/source/command-line_interface) according to the following syntax:
 sum [OPTION]... [FILE]...

with the possible option parameters being:
* {{mono|-r}}
** use [BSD checksum](/source/BSD_checksum) algorithm, use 1K blocks (defeats {{mono|-s}})
* {{mono|-s}}, {{mono|--sysv}}
** use [SYSV checksum](/source/SYSV_checksum) algorithm, use 512 bytes blocks
* {{mono|--help}}
** display the help screen and exit
* {{mono|--version}}
** output version information and exit

When no file parameter is given, or when FILE is {{mono|-}}, the [standard input](/source/standard_input) is used as input file.

Example of use:
<syntaxhighlight lang="console">
$ echo Hello > testfile
$ sum testfile
36978     1
</syntaxhighlight>

Example of -s use in GNU sum:
<syntaxhighlight lang="console">
$ echo Hello > testfile
$ sum -s testfile
510 1 testfile
</syntaxhighlight>

Example of using standard input, -r and printf to avoid newline:
<syntaxhighlight lang="console">
$ printf Hello | sum -r
08401     1
</syntaxhighlight>

==See also==
* [GNU Core Utilities](/source/GNU_Core_Utilities)
* [UnxUtils](/source/UnxUtils) port to native Win32

==References==
{{Reflist}}

==External links==
* [https://pubs.opengroup.org/onlinepubs/007908799/xcu/sum.html sum] in The Single UNIX ® Specification, Version 2, 1997, opengroup.org – does not specify checksum algorithm
* [https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap04.html C.4 Utilities] in The Open Group Base Specifications Issue 7, 2018 edition, opengroup.org – indicates sum as removed
* {{man|1|sum|v7}}
* {{man/format|1|sum|https://www.gnu.org/software/coreutils/manual/html_node/sum-invocation.html}} – manual pages from [GNU](/source/GNU) [coreutils](/source/coreutils)
* {{man|1|sum|FreeBSD}} – states the sum algorithms in options -o1 and -o2 to cksum
* {{man|1|sum|Linux}}
* {{man|1|sum|Solaris}}
* [https://www.tuhs.org/cgi-bin/utree.pl The Unix Tree], tuhs.org – the source code of old versions of Unix

{{Unix commands}}
{{Core Utilities commands}}

Category:Linux package management-related software
Category:Unix package management-related software
Category:Linux security software
Category:Unix security-related software

---
Adapted from the Wikipedia article [Sum (Unix)](https://en.wikipedia.org/wiki/Sum_(Unix)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Sum_(Unix)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
