Linux Command Reference ยท Viewing and Searching Files
Linux md5sum and sha256sum commands
Reference for md5sum and sha256sum on Linux: compute file checksums, verify downloads with -c, read the output format and choose a safe hash algorithm.
md5sum and sha256sum compute a checksum (a fixed-length fingerprint) of a file's content. Two files with identical content always have the same checksum, and a change of even one bit produces a completely different one. That makes checksums the standard way to check that a download is not corrupted, that a backup copied correctly, or that two files on different machines match.
They belong to a family: sha1sum, sha224sum, sha256sum, sha384sum, sha512sum and b2sum all behave the same way and differ only in the algorithm.
sha256sum [OPTION]... [FILE]...
sha256sum -c CHECKFILE
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
md5sum [OPTION]... [FILE]...
sha256sum [OPTION]... [FILE]...
sha256sum -c [OPTION]... [CHECKFILE]
With no FILE or with -, the data is read from standard input.
Output format: <hex digest> <file name>, two spaces apart (the second is a mode marker: space for text mode, * for binary mode; on Linux both read the same bytes). MD5 digests have 32 hex digits, SHA-256 digests have 64.
| Option | Description |
|---|---|
-c, --check |
Read checksums from the files (or stdin) and verify them. |
--quiet |
With -c, do not print OK lines. |
--status |
With -c, print nothing at all; only the exit status tells the result. |
-w, --warn |
With -c, warn about badly formatted lines. |
--ignore-missing |
With -c, skip files that do not exist instead of failing. |
--strict |
With -c, fail on any improperly formatted line. |
-b, -t |
Binary or text mode marker (no difference on Linux). |
-z |
End output lines with NUL, not newline. |
--tag |
Print BSD-style lines: SHA256 (file) = digest. |
Returns: 0 when all files were read (or all verified), 1 when a check failed or a file could not be read.
Usage notes
- Which algorithm. MD5 and SHA-1 are broken against deliberate attackers (collisions can be manufactured), but are fine for detecting accidental corruption. For anything security-related, such as verifying a download, use
sha256sumor stronger, and get the expected value from a trustworthy channel. - A checksum is not a signature. If an attacker can replace both the file and the checksum next to it, verification proves nothing. Signed checksum files (GPG) close that gap.
- Content only. The name, permissions and timestamps do not influence the digest. Two differently named copies hash equally.
- Whole directories. The tools hash files, not directories. Combine them:
find . -type f -print0 | sort -z | xargs -0 sha256sumlists a digest per file, and hashing that list gives one digest for the tree. - Find duplicates.
sha256sum * | sort | uniq -w64 -dprints files whose content is the same as another (uniq -w64compares the first 64 characters). - Check file format. In a checksum file each line must be
digest, two spaces, the name. A file name that begins with a backslash or contains a newline is escaped by the tool. - Speed. Hashing is limited by disk speed on large files; SHA-256 is slower than MD5 on small CPUs but fast enough almost everywhere.
b2sum(BLAKE2) is often faster than both. - macOS and BSD name the tools
md5andshasum -a 256; the GNU commands here are the Linux ones.
Related
cmp,diff,split- Lessons: diff, comm and patch, Archives