Operations 4 min read

How to Verify File Integrity with md5sum and Automate Checks on Linux

This guide explains how to generate MD5 checksums for files, compare them to detect changes, save checksum lists, and use md5sum's verification options—including quiet and status modes—to script bulk integrity checks on Linux systems.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Verify File Integrity with md5sum and Automate Checks on Linux

The md5sum command generates an MD5 hash for a file, allowing you to verify whether the file's contents have changed and to compare two files for exact equality. Since it reads only file contents, it cannot detect changes to file attributes.

# Copy the original file for testing
cp -a /etc/fstab /tmp/fstab
cp -a /etc/fstab /tmp/fstab1

Generate MD5 hashes for the two copies:

# Compute checksums
md5sum /tmp/fstab /tmp/fstab1

The identical hashes indicate that the files are exactly the same.

Because each hash is followed by the file path, you can store the output in a file and later use it to verify whether the files have been altered.

# Save checksums to a file
md5sum /tmp/fstab /tmp/fstab1 > /tmp/fs.md5sum
# Verify the files against the saved list
md5sum -c /tmp/fs.md5sum

The verification reports "OK" for both files, confirming that their contents match the recorded hashes. Changes to permissions or attributes do not affect the MD5 value.

If you modify one of the files, the verification will detect the difference:

# Modify the second file
echo aaa >> /tmp/fstab1
# Re‑run verification
md5sum -c /tmp/fs.md5sum

The output shows /tmp/fstab: OK and /tmp/fstab1: FAILED, with a warning that one of the two computed checksums did not match.

--quiet Do not display records that are OK. --status Suppress all output; the command’s exit status indicates success (0) or failure (1).
# Use status mode
md5sum --status -c /tmp/fs.md5sum
# Check the exit code
echo $?

The exit code is 1 because a checksum failed. This approach lets you script bulk integrity checks for many files, automating detection of any content changes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxshell scriptchecksumfile integrityverificationmd5sum
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.