Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
md5sum Command in Linux with Examples
The md5sum command in Linux generates MD5 hash values (checksums) for files or text input. MD5 (Message Digest Algorithm 5) is a cryptographic hash function that produces a 128-bit hash value, typically represented as a 32-character hexadecimal string. This command is essential for verifying file integrity and detecting changes in data.
The MD5 algorithm creates a unique fingerprint for data. Even a single character change results in a completely different hash value, making it useful for detecting file corruption or unauthorized modifications.
Basic Syntax
md5sum [OPTION] [FILE]... md5sum [OPTION] --check [FILE]
Examples
Generating Hash for Text Input
You can generate MD5 hash values for text strings using pipes with the echo command −
sh-4.4$ echo 'Hello World' Hello World sh-4.4$ echo 'Hello World' | md5sum e59ff97941044f85df5297e1c302d260 - sh-4.4$
The dash (-) indicates that the input came from standard input (stdin) rather than a file.
Demonstrating Hash Sensitivity
Even minor changes in input produce completely different hash values. Notice the difference when we change 'W' to 'w' −
sh-4.4$ echo 'Hello World' | md5sum e59ff97941044f85df5297e1c302d260 - sh-4.4$ echo 'Hello world' | md5sum f0ef7081e1539ac00ef5b761b4fb01b3 - sh-4.4$
Generating Hash for Files
To generate MD5 checksum for files, simply specify the filename as an argument −
sh-4.4$ cat sample.txt This is sample text sh-4.4$ md5sum sample.txt 7d01d6303e91e880ad6ad10a7aa10537 sample.txt sh-4.4$
Multiple Files
sh-4.4$ md5sum file1.txt file2.txt file3.txt d85b1213473c2fd7c2045020a6b9c62b file1.txt 098f6bcd4621d373cade4e832627b4f6 file2.txt 5d41402abc4b2a76b9719d911017c592 file3.txt
Common Use Cases
| Use Case | Command | Purpose |
|---|---|---|
| File Integrity Check | md5sum filename |
Generate checksum to verify file hasn't changed |
| Verify Downloads | md5sum downloaded_file |
Compare with provided checksum |
| Create Checksum File | md5sum * > checksums.md5 |
Save checksums for later verification |
| Verify Checksums | md5sum -c checksums.md5 |
Check files against saved checksums |
Creating and Verifying Checksum Files
# Create checksum file sh-4.4$ md5sum *.txt > checksums.md5 sh-4.4$ cat checksums.md5 7d01d6303e91e880ad6ad10a7aa10537 sample.txt d85b1213473c2fd7c2045020a6b9c62b file1.txt # Verify checksums sh-4.4$ md5sum -c checksums.md5 sample.txt: OK file1.txt: OK
Key Points
MD5 produces a 128-bit hash represented as 32 hexadecimal characters
Any change in input data results in a dramatically different hash
The
-coption verifies files against existing checksum filesMD5 is fast but not cryptographically secure for sensitive applications
For security purposes, consider using
sha256sumorsha512suminstead
Conclusion
The md5sum command is a valuable tool for generating MD5 checksums to verify file integrity and detect changes in data. While MD5 is suitable for basic integrity checks, it's recommended to use stronger hash algorithms like SHA-256 for security-sensitive applications.
