Beginner’s Guide to the Linux `sum` Command: Tips and Examples

As you delve deeper into working with the Linux command line, you will come across some utilities that aren’t frequently used but can be quite handy. One such tool is sum. This utility provides two features: displaying checksum and block count for input files. In this brief tutorial, we’ll explore the basics of the sum command through straightforward examples.

It’s important to note that all examples in this tutorial have been tested on an Ubuntu 24.04 LTS machine.

Overview of the Linux sum Command

The sum command helps you obtain information about a file’s checksum and its block count. Here’s the syntax for the command:

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

The command’s manual page describes it simply as:

Print checksum and block counts for each FILE.

Below are some Q&A examples to demonstrate how the sum command can be used:

Q1. How can you use the sum command?

Using the command is quite simple—just provide an input file to the command. For instance:

sum file1

The output generated on my system was:

54333    1

The first number is the checksum, and the second number represents the block count of the file.

Q2. Can sum accept input from STDIN?

Yes, to allow sum to accept input from standard input, just execute the command without any file:

sum

After pressing Enter, the command will wait for input:

How to make sum accept input from STDIN

Once you provide the input file name, press Ctrl+D to receive the output:

sum command processes input file

Q3. How do you change the algorithm used by sum?

The tool provides two options: -r and -s. The -r option enables the BSD checksum algorithm, while the -s option switches to the System V algorithm. Also, note that -r uses 1K blocks, while -s works with 512-byte blocks.

Example usage is depicted below:

How to change algorithm sum uses

For more detailed information about sum, use the standard –help and –version options, which display comprehensive help information and version details, respectively.

Conclusion

The sum command is user-friendly with an easy learning curve. We’ve covered nearly all the command-line options that it offers. For further details, refer to the man page.

FAQ

What does the checksum represent?

A checksum is a value used to verify the integrity of a file or data transfer. It ensures that the data has not been corrupted during transmission or storage.

Why would I use the BSD vs. System V option?

The choice between BSD or System V depends on the compatibility requirements of the systems you are working with. BSD uses a 1K block size, while System V uses 512 bytes, potentially affecting performance and compatibility.

Can I use the sum command on directories?

No, the sum command is designed to work with files, not directories. If you try to use it on a directory, it will likely return an error.