sum

file integrityLinux/Unix
The sum command is one of the most frequently used commands in Linux/Unix-like operating systems. sum Compute checksum and count blocks in a file

Quick Reference

Command Name:

sum

Category:

file integrity

Platform:

Linux/Unix

Basic Usage:

sum [options] [arguments]

Common Use Cases

    Syntax

    sum [options] [file...]

    Options

    Option Description
    -r Use BSD sum algorithm (default)
    -s, --sysv Use System V sum algorithm
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the sum command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    sum file.txt
    Compute and display the BSD checksum and block count of file.txt.
    sum -r file.txt
    Compute and display the SYSV (System V) checksum and block count.
    sum file1.txt file2.txt file3.txt
    Compute checksums for multiple files. # Advanced Examples Advanced # Process standard input
    cat file.txt | sum
    # Compare checksums of two files if [ "$(sum file1.txt)" = "$(sum file2.txt)" ]; then echo "Files have identical checksums" else echo "Files have different checksums" fi # Sum all text files in current directory sum *.txt # Check if file has been modified by saving the original checksum original_sum=$(sum file.txt) # ... later check if file changed current_sum=$(sum file.txt) if [ "$original_sum" != "$current_sum" ]; then echo "File has been modified" fi # Use sum with find to calculate checksums for many files find . -type f -name "*.c" -exec sum {} \; # Calculate sums for all files in directory and save to a log sum * > checksums.log # Use with sort to get a sorted list of checksums sum * | sort -n # Calculate checksums and grep for specific values sum * | grep "^12345" # Verify a file hasn't been corrupted by comparing checksums echo "12345 1 file.txt" > expected_sum.txt sum file.txt | diff - expected_sum.txt && echo "File is intact"

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `sum` command is a basic file integrity checking utility found in Unix and Linux systems. It calculates and prints a checksum for each file specified on the command line, along with the number of blocks in the file. If no file is specified, it reads from standard input. While `sum` is one of the oldest checksum utilities in Unix, it has largely been superseded by more robust alternatives like `md5sum`, `sha1sum`, and other modern hash functions that provide stronger integrity guarantees. However, `sum` remains available for compatibility with older systems and scripts. The command supports two different checksum algorithms: 1. BSD algorithm (default): This is the traditional algorithm used in BSD Unix variants. It uses a simple 16-bit checksum. 2. System V algorithm: This algorithm is used in System V Unix variants and can be selected with the `-s` or `--sysv` option. The output format of the `sum` command is: ``` checksum blocks filename ``` Where: - `checksum` is the calculated checksum value - `blocks` is the number of 512-byte blocks in the file - `filename` is the name of the file that was checksummed The primary uses of the `sum` command include: - Basic integrity checking of files to detect accidental corruption - Comparing files to see if they're identical (though more robust tools are preferred for this purpose) - Compatibility with legacy scripts and systems that expect `sum` checksums - Quick verification when stronger hash functions aren't necessary It's important to note that the `sum` command provides only basic error detection capabilities and is not suitable for security-critical applications or for detecting malicious tampering. The simple algorithms it uses can be easily manipulated, and collisions (different files producing the same checksum) are relatively common. For more robust file integrity checking or for security purposes, modern alternatives like `md5sum`, `sha1sum`, `sha256sum`, or `cksum` are strongly recommended. These provide stronger guarantees against both accidental corruption and deliberate tampering.

    Related Commands

    These commands are frequently used alongside sum or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the sum command works in different scenarios.

    $ sum
    View All Commands