sum - Linux
Overview
The sum
command in Linux is a utility for calculating a checksum for a file. Primarily, it computes a checksum and the count of blocks in a file, facilitating the integrity check of files, especially before and after transfer across networks. It is most effectively used for verifying that files have not been corrupted or altered during transfer.
Syntax
The general usage of the sum
command is as follows:
sum [OPTION]... [FILE]...
FILE
: Specifies the file or files for which the checksum is computed. If no file is specified or if the input is-
, it reads from standard input.
Usage Variations:
- Compute checksum of a single file:
sum filename
- Compute checksum for multiple files:
sum file1 file2 file3
- Read from standard input:
some_command_output | sum
Options/Flags
-r
: Use BSD-style checksum algorithm and block counting. This is the default behavior.-s, --sysv
: Use System V style checksum algorithm and block counting.
The choice of checksum style (-r
vs -s
) impacts the checksum calculation and output format. Default behavior can vary based on system configuration.
Examples
-
Basic Usage:
Calculate the checksum and block count of a file using the default (BSD) method:sum myfile.txt
-
Using System V Style:
Calculate checksum using the System V style:sum -s myfile.txt
-
Multiple Files:
Compute checksums for multiple files:sum file1.txt file2.txt file3.txt
-
Using Pipes:
Compute the checksum for output of another command:cat myfile.txt | sum
Common Issues
- Binary Files: The
sum
command may produce inconsistent results with binary files across different platforms or versions of the utility. - Large Files: Performance issues may arise when dealing with extremely large files.
Solutions:
- Use other checksum tools like
md5sum
orsha1sum
for binary or large files for better reliability and performance.
Integration
Combine sum
with other commands to automate integrity checks:
# Example script to verify checksum before and after transfer
ORIG_SUM=$(sum original_file | awk '{print $1}')
scp original_file user@destination:/path/to/destination
REMOTE_SUM=$(ssh user@destination "sum /path/to/destination/original_file" | awk '{print $1}')
if [ "$ORIG_SUM" -eq "$REMOTE_SUM" ]; then
echo "File integrity verified."
else
echo "Checksum mismatch."
fi
Related Commands
cksum
: Provides a CRC checksum and byte count, generally more robust for checking file integrity.md5sum
,sha1sum
, etc.: Computations of MD5 and SHA-1 hashes, respectively, suitable for robust cryptographic verifications.
Further details and differences between these utilities can be studied in their respective man pages (man cksum
, man md5sum
).