Text processing
md5sum
Compute or verify MD5 checksums; fine for spotting accidental changes, not for security.
md5sum [OPTION...] [FILE...]
md5sum prints the MD5 hash of each file: 32 hex digits, two spaces and the name. It works
exactly like sha256sum, including -c to verify a saved list, and reads standard input
when no file is given.
MD5 is cryptographically broken: it is easy to construct two different files with the same MD5
hash. It is still fine for catching accidental corruption and for finding duplicate files,
and you will meet it in older download pages, package metadata and cloud storage ETags. For
anything where someone might tamper with the data on purpose, use sha256sum.
Examples
Hash a file
md5sum todo.txt
70472651797642aa9a78f7cbfb9416c5 todo.txt
The 32-digit hex digest, two spaces, then the file name.
Hash several files
md5sum photos/*.png
c2e73eb75388960ec708ffdec1b07100 photos/IMG_2041.png 94c52963b91dc4d437b2dabc0e2f2253 photos/beach.png f76762589d1a641cba99b6ddf9966df6 photos/forest.png
One line per file, ready to be saved as a checklist.
Save and verify checksums
md5sum logs/*.log > logs.md5
md5sum -c logs.md5
logs/app.log: OK logs/error.log: OK
-c recomputes each hash from the list and prints OK for each file that still matches.
A file that changed
md5sum todo.txt > todo.md5
echo 'call the bank' >> todo.txt
md5sum -c todo.md5
todo.txt: FAILED md5sum: WARNING: 1 computed checksum did NOT match
Any change to the file changes its hash, so the check reports FAILED and the command exits with 1.
Find duplicate files
Setup run before this example
cp photos/beach.png photos/beach-copy.png
cp notes.txt notes-backup.txtmd5sum *.txt photos/*.png | sort | uniq -w 32 -D
94c52963b91dc4d437b2dabc0e2f2253 photos/beach-copy.png 94c52963b91dc4d437b2dabc0e2f2253 photos/beach.png c4bdac40a6d8b7dee028ccb8dc4f04fa notes-backup.txt c4bdac40a6d8b7dee028ccb8dc4f04fa notes.txt
Files with identical content have identical hashes. Sorting brings them together and
uniq -w 32 -D prints every line whose first 32 characters (the hash) repeat: the two
copies of the notes and the two beach pictures.
Hash a string
printf 'hello' | md5sum
5d41402abc4b2a76b9719d911017c592 -
With no file, md5sum hashes standard input and prints - as the name. Use printf, not echo, so no newline is hashed.
Check that a copy is identical
cp access.log /tmp/access.log
md5sum access.log /tmp/access.log
2807586b63933d6cf3c3320341980306 access.log 2807586b63933d6cf3c3320341980306 /tmp/access.log
The same hash for both files confirms the copy is intact. For a plain yes/no on two local files, cmp is faster and exact.
A listed file that no longer exists
md5sum notes.txt > notes.md5
mv notes.txt meeting-notes.txt
md5sum -c notes.md5
md5sum: notes.txt: No such file or directory notes.txt: FAILED open or read md5sum: WARNING: 1 listed file could not be read
Checksum lists store names, not content locations. After the rename, md5sum -c cannot
open notes.txt, reports FAILED open or read and exits with 1.
Common pitfalls
MD5 collisions can be produced deliberately. Don't use MD5 to verify downloads from untrusted sources, sign data or store passwords.
echo text | md5sumincludes the newline. Useprintf '%s' text | md5sum.File names in a checksum list are relative to the directory where it was made. Run
md5sum -cfrom that same directory.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
macOS uses
md5instead, which printsMD5 (file) = hash;md5 -rprints the GNU-style hash-then-name order.md5sum --tagproduces the BSD style on Linux.
Options worth knowing
A selection, described in our own words. For the full list, see md5sum(1) or md5sum --help.
| Option | What it does |
|---|---|
-c, --check | Verify files against the checksums listed in FILE. |
--quiet | With -c, don't print OK lines. |
--status | With -c, print nothing; use the exit status. |
--ignore-missing | With -c, skip files that do not exist. |
--strict | With -c, fail on improperly formatted lines. |
--tag | Print BSD-style lines: MD5 (file) = hash. |
-b, --binary | Binary mode; no difference on Linux. |
-z, --zero | End output lines with NUL instead of newline. |
Related
sha256sumCompute SHA-256 checksums of files, or verify files against a list of checksums with -c.cmpCompare two files byte by byte and report the first difference, or just whether they differ.uniqCollapse or report repeated adjacent lines; with sort, count how often each line occurs.sortSort lines of text alphabetically, numerically, by version or by chosen fields; also merge and de-duplicate.