Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

sum (Unix)

From Wikipedia, the free encyclopedia
Unix command
sum
Original authorKen Thompson
DeveloperAT&T Bell Laboratories
Initial releaseNovember 3, 1971; 54 years ago (1971-11-03)
Operating systemUnix,Unix-like,Inferno
PlatformCross-platform
TypeCommand
Licensecoreutils:GPLv3+

sum is a legacy utility available on someUnix andUnix-likeoperating systems. This utility outputs a 16-bitchecksum of each argumentfile, as well as the number ofblocks they take on disk.[1] Two different checksum algorithms are in use. POSIX abandonedsum in favor ofcksum.

Overview

[edit]

Thesum program is generally only useful for historical interest. It is not part of POSIX. Two algorithms are typically available: aBSD checksum and aSYSV checksum. Both are weaker than the already weak 32-bit CRC used bycksum.[2]

Thedefault algorithm on FreeBSD and GNU implementations is the BSD checksum. Switching between the two algorithms is done via command line options.[2][1]

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 inSeventh 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

[edit]

Thesum utility is invoked from thecommand line according to the following syntax:

sum [OPTION]... [FILE]...

with the possible option parameters being:

  • -r
  • -s,--sysv
  • --help
    • display the help screen and exit
  • --version
    • output version information and exit

When no file parameter is given, or when FILE is-, thestandard input is used as input file.

Example of use:

$echoHello>testfile$sumtestfile36978     1

Example of -s use in GNU sum:

$echoHello>testfile$sum-stestfile510 1 testfile

Example of using standard input, -r and printf to avoid newline:

$printfHello|sum-r08401     1

See also

[edit]

References

[edit]
  1. ^absum(1) — manual pages fromGNUcoreutils
  2. ^absum(1) – FreeBSD General CommandsManual

External links

[edit]
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Text utilities
Shell utilities
Retrieved from "https://en.wikipedia.org/w/index.php?title=Sum_(Unix)&oldid=1132116168"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp