md5 - macOS


Overview

The md5 command calculates and prints the MD5 checksum (a 128-bit hexadecimal number) for a file, text, or standard input data. It is widely used for verifying data integrity and checking for file corruption.

Syntax

md5 [options] [input_file]...

Options/Flags

  • -r, –recursive: Calculate MD5 for all files in the given directory and its subdirectories.
  • -q, –quiet: Suppress output, print only the checksum.
  • -c, –check: Check MD5 checksums against a checksum file.
  • -s, –skip-failed: Skip files that cannot be opened or read.
  • -b, –binary: Calculate MD5 for binary files without considering line breaks.
  • -t, –text: Treat input files as text (default).
  • -h, –help: Print help and usage information.
  • -V, –version: Print version information and exit.

Examples

  • Calculate MD5 for a single file:
    md5 /path/to/file.txt
    
  • Calculate MD5 for multiple files:
    md5 file1.txt file2.txt file3.txt
    
  • Calculate MD5 for a directory and its subdirectories:
    md5 -r /path/to/directory
    
  • Suppress output and print only the checksum:
    md5 -q /path/to/file.txt
    
  • Check file integrity using an MD5 checksum:
    md5 -c checksum.txt
    

Common Issues

  • File access errors: If md5 cannot open or read a file, check permissions and make sure the file exists.
  • Invalid checksum file: When using the -c option, ensure the checksum file contains valid MD5 checksums.
  • Non-UTF8 files: UTF8 is the default encoding. Non-UTF8 files may produce incorrect checksums. Use the -b flag for binary files.

Integration

  • Use grep to filter md5 results based on the checksum:
    md5 file.txt | grep checksum
    
  • Create a bash script to check the integrity of multiple files using md5:
    #!/bin/bash
    for file in *.txt; do
      md5 -c checksum.txt $file
    done
    
  • sha1sum: Calculate SHA1 hashes.
  • sha256sum: Calculate SHA256 hashes.
  • openssl: A comprehensive cryptography utility that includes MD5 calculations.