md5sum - Linux


Overview

The md5sum command in Linux generates and checks MD5 (128-bit) cryptographic hash values. The primary purpose of md5sum is to ensure data integrity, by providing a checksum for file validation. It’s heavily utilized to verify that files downloaded from the internet are not corrupted or tampered with.

Syntax

The basic syntax of the md5sum command is:

md5sum [OPTION]... [FILE]...

If no FILE is specified, or if FILE is -, md5sum reads from the standard input.

Options/Flags

  • -b, –binary: Read in binary mode (default on non-text files).
  • -c, –check: Read MD5 sums from the FILEs and check them.
  • -t, –text: Read in text mode (default unless the binary mode is specified).
  • –tag: Create a BSD-style checksum.
  • -z, –zero: End each output line with NUL, not newline, when checking.
  • –quiet: Don’t print OK for each successfully verified file.
  • –status: Don’t output anything, status code shows success.
  • -w, –warn: Warn about improperly formatted checksum lines.
  • –strict: Exit non-zero for improperly formatted checksum lines.
  • –help: Display a help message and exit.
  • –version: Output version information and exit.

Examples

1. Calculating the checksum of a file:

md5sum filename.txt

2. Verifying checksums from a file:

md5sum -c checksums.md5

3. Checksum multiple files and output to a file:

md5sum file1.txt file2.txt > checksums.md5

4. Using md5sum in a pipeline:

echo "example" | md5sum

Common Issues

  • Binary vs Text Mode: On Windows, forgetting to use the -b flag on binary files might result in incorrect checksums due to different line ending interpretations.
  • File Access: md5sum might produce errors if it lacks permission to access a file. Ensure you have the proper permissions or run with elevated privileges if necessary.
  • Corrupted Checksum File: A manual modification of a checksum file can result in format errors, adhering strictly to the expected format is crucial for the -c option.

Integration

md5sum can be coupled with other commands to facilitate powerful workflows:

find . -type f -name "*.txt" -exec md5sum {} + > checksums.md5

This command finds all .txt files in current and subdirectories and calculates their MD5 sums, storing the results in checksums.md5.

  • sha1sum: Similar to md5sum but uses the SHA-1 hash algorithm.
  • sha256sum: Uses SHA-256, providing more security than MD5 for cryptographic purposes.
  • cksum: Provides checksums and byte counts for files, using a simpler CRC method compared to MD5.

For more information, consider looking into the official GNU Core Utilities documentation at GNU Coreutils.