Only get hash value using md5sum (without filename)

I use md5sum to generate a hash value for a file. But I only need to receive the hash value, not the file name.

md5=`md5sum ${my_iso_file}`
echo ${md5}

Output:

3abb17b66815bc7946cefe727737d295  ./iso/somefile.iso

How can I 'strip' the file name and only retain the value?


Solution 1:

A simple array assignment works... Note that the first element of a Bash array can be addressed by just the name without the [0] index, i.e., $md5 contains only the 32 characters of md5sum.

md5=($(md5sum file))
echo $md5
# 53c8fdfcbb60cf8e1a1ee90601cc8fe2

Solution 2:

Using AWK:

md5=`md5sum ${my_iso_file} | awk '{ print $1 }'`

Solution 3:

You can use cut to split the line on spaces and return only the first such field:

md5=$(md5sum "$my_iso_file" | cut -d ' ' -f 1)

Solution 4:

On Mac OS X:

md5 -q file