Drathier
Drathier

Reputation: 14539

One-liner in ubuntu/macos to print checksum of a file

Is there a short one-liner to get a file checksum, which works on both macos and ubuntu? It doesn't matter what algorithm or program, as long as I don't have to install or setup anything.

Upvotes: 1

Views: 1528

Answers (5)

brunorey
brunorey

Reputation: 2255

Just try both of them:

md5 file 2>/dev/null; md5sum file 2>/dev/null;

That line will work on both OSs, running both commands and discarding the one that gives an error, it will print only the valid result.

Upvotes: 2

l'L'l
l'L'l

Reputation: 47284

You could use OpenSSL, and the commands should be the same:

openssl sha256 filename | awk -F'= ' '{print $2}' # optional

Use whatever hashing algorithm you want, sha256, sha1, md5, etc.

Upvotes: 4

tripleee
tripleee

Reputation: 189910

May I be so impertinent as to suggest writing your own?

python -c 'import sys, hashlib;
m = hashlib.sha256(); 
m.update(open(sys.argv[1]).read());
print("\t".join([m.hexdigest(), sys.argv[1]]))' file

The semicolons are gratuitous here, but necessary if you really want to force the issue and make this a literal one-liner.

Upvotes: 0

thomasgustavo
thomasgustavo

Reputation: 91

On Linux, you can use md5sum file; on macOS, just md5 file. Both are default at a clean install, AFAIK. If you require that the command be the same, you can create an alias.

Upvotes: 1

petertag
petertag

Reputation: 61

With a quick OS check you can use either md5 (mac) or md5sum (ubuntu), alternatively you could alias one of them so you'd be using the same command on either OS.

Upvotes: 0

Related Questions