Skip to main content

How to Get the MD5 Hash of a File in the Terminal

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

An MD5 hash is a 32-character fingerprint of a file's contents: the same contents always give the same hash, and changing even one character gives a different one. On macOS, md5 <file> prints it, md5sum <file> prints it in the format Linux uses, and in PowerShell, Get-FileHash -Algorithm MD5 <file> does the same.

This guide hashes three small text files, with real output from macOS 26 and PowerShell 7.6.10.

Syntax​

Syntax
md5 [-q] <file>...
md5sum <file>...
Syntax
Get-FileHash -Algorithm MD5 <file>
PartWhat it means
md5The macOS command for MD5 hashes
-qOptional. Prints only the hash, without the file's name
md5sumThe same hash, in the format of the GNU command of the same name, which Linux has. macOS has it too
<file>...One or more files, separated by spaces
Get-FileHashThe PowerShell command for a file's hash
-Algorithm MD5Which kind of hash to make. Without it, Get-FileHash makes a SHA-256 hash
<file>In PowerShell, one file, or several separated by commas

The macOS manual page, man md5, describes these commands as tools that calculate a message-digest fingerprint (checksum) for a file, and says md5sum has options and an output format that match the GNU commands of the same name. Microsoft's documentation describes Get-FileHash in one line: Computes the hash value for a file by using a specified hash algorithm.

The Example​

A folder, md5-demo, holds three small text files: notes.txt with the line Buy milk, notes-copy.txt, an exact copy of it, and notes-new.txt with the line Buy milk and bread. You hash them and compare the hashes.

Step 1: Create the Three Files​

Terminal
mkdir md5-demo
cd md5-demo
echo "Buy milk" > notes.txt
cp notes.txt notes-copy.txt
echo "Buy milk and bread" > notes-new.txt

mkdir creates the folder and cd moves into it. Each echo writes its line to a new file, and cp copies notes.txt. None of them prints anything.

Step 2: Get a File's MD5 Hash on macOS with md5​

Terminal
md5 notes.txt
Output
MD5 (notes.txt) = 33a737dc0a68f8d623a4d53f935d6650

md5 prints the kind of hash, the file's name in brackets, and the hash: 32 characters, each a digit or a letter from a to f.

To get only the hash, for example to use it in a script, add -q:

Terminal
md5 -q notes.txt
Output
33a737dc0a68f8d623a4d53f935d6650

The terminal after md5 notes.txt, which prints MD5 (notes.txt) = 33a737dc0a68f8d623a4d53f935d6650, and md5 -q notes.txt, which prints only the hash.

The manual calls -q quiet mode: only the checksum is printed out.

Step 3: Get It in the Linux Format with md5sum​

Terminal
md5sum notes.txt
Output
33a737dc0a68f8d623a4d53f935d6650 notes.txt

The terminal after md5sum notes.txt, which prints the hash 33a737dc0a68f8d623a4d53f935d6650, two spaces and the file name.

The same hash, with the hash first, then two spaces and the file's name. That's the format of md5sum on Linux, so a command written this way works on both.

Step 4: Get It in PowerShell with Get-FileHash​

PowerShell
Get-FileHash -Algorithm MD5 notes.txt
Output
Algorithm Hash Path
--------- ---- ----
MD5 33A737DC0A68F8D623A4D53F935D6650 /Users/you/source/md5-demo/notes.txt

PowerShell prints a table: the kind of hash, the hash, and the file's full path. The hash is the same one, in capital letters. A hash is written in hexadecimal, where a and A stand for the same value, so 33a737dc… and 33A737DC… are the same hash.

To get only the hash, put the command in brackets and add .Hash:

PowerShell
(Get-FileHash -Algorithm MD5 notes.txt).Hash
Output
33A737DC0A68F8D623A4D53F935D6650

The terminal, running PowerShell, after Get-FileHash -Algorithm MD5 notes.txt, which prints a table with MD5, the hash 33A737DC0A68F8D623A4D53F935D6650 and the file's path, and after (Get-FileHash -Algorithm MD5 notes.txt).Hash, which prints only the hash.

Get-FileHash returns an object with three properties, Algorithm, Hash and Path, and .Hash reads one of them.

Step 5: Compare Files by Their Hashes​

Give the command all three files:

Terminal
md5 notes.txt notes-copy.txt notes-new.txt
Output
MD5 (notes.txt) = 33a737dc0a68f8d623a4d53f935d6650
MD5 (notes-copy.txt) = 33a737dc0a68f8d623a4d53f935d6650
MD5 (notes-new.txt) = 5b3b46ace339a564ed4d83ae12a53472
Terminal
md5sum notes.txt notes-copy.txt notes-new.txt
Output
33a737dc0a68f8d623a4d53f935d6650 notes.txt
33a737dc0a68f8d623a4d53f935d6650 notes-copy.txt
5b3b46ace339a564ed4d83ae12a53472 notes-new.txt

The terminal after md5 and md5sum on the three files: notes.txt and notes-copy.txt both have the hash 33a737dc0a68f8d623a4d53f935d6650, and notes-new.txt has 5b3b46ace339a564ed4d83ae12a53472.

In PowerShell, separate the files with commas:

PowerShell
Get-FileHash -Algorithm MD5 notes.txt, notes-copy.txt, notes-new.txt
Output
Algorithm Hash Path
--------- ---- ----
MD5 33A737DC0A68F8D623A4D53F935D6650 /Users/you/source/md5-demo/notes.txt
MD5 33A737DC0A68F8D623A4D53F935D6650 /Users/you/source/md5-demo/notes-copy.txt
MD5 5B3B46ACE339A564ED4D83AE12A53472 /Users/you/source/md5-demo/notes-new.txt

The original and its copy have the same hash, because their contents are the same, even though their names differ. notes-new.txt has a completely different hash, although it only has two more words. Microsoft's documentation puts it this way: If the hash values of two files are identical, the contents of the files are also identical.

Step 6: See What Happens When the File Isn't There​

A typo, note.txt instead of notes.txt:

Terminal
md5 note.txt
Output
md5: note.txt: No such file or directory
Terminal
md5sum note.txt
Output
md5sum: note.txt: No such file or directory

The terminal after md5 note.txt and md5sum note.txt, which print md5: note.txt: No such file or directory and md5sum: note.txt: No such file or directory.

PowerShell
Get-FileHash -Algorithm MD5 note.txt
Output
Get-FileHash: Cannot find path '/Users/you/source/md5-demo/note.txt' because it does not exist.

Each command names the file it couldn't find. Both md5 and md5sum exit with status 1 here, which the manual explains: they exit 0 on success, 1 if at least one of the input files could not be read.

Common Mistakes​

MistakeWhat happensDo this instead
Get-FileHash notes.txt, without -Algorithm MD5You get a SHA-256 hash, 64 characters long, not the MD5 hashAdd -Algorithm MD5
Comparing a PowerShell hash with an md5 hash letter by letterThe letters differ in case, so the hashes look differentCompare them without regard to case: they're the same hash
Using MD5 to protect files from tamperingMD5 isn't secure against attackUse MD5 only to spot changes; use SHA-256 for security

FAQ​

Which hash does Get-FileHash make without -Algorithm?​

SHA-256. (Get-FileHash notes.txt).Algorithm printed SHA256, and Microsoft's documentation says: If no value is specified, or if the parameter is omitted, the default value is SHA256.

Is MD5 safe to use?​

To spot changes, yes. For security, no. Microsoft's documentation says MD5 and SHA1, which are no longer considered secure, should only be used for simple change validation, and the macOS manual warns that MD5 is vulnerable to practical collision attacks: someone can make two different files with the same hash on purpose.

Should I use md5 or md5sum on macOS?​

Either gives the same hash. md5sum prints the format Linux uses, so it's the better choice for commands that also run on Linux.

Conclusion​

md5 notes.txt printed the MD5 hash of notes.txt, md5 -q printed only the hash, md5sum printed it in the Linux format, and Get-FileHash -Algorithm MD5 printed it in PowerShell, in capital letters. A file and its copy had the same hash, and a changed file had a new one, which is what makes a hash useful for spotting changes.

Additional Resources​