203
votes

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?

16
Very surprising this isn't an option for md5sum.Sridhar Sarnobat
Agreed! Why isn't this an option? Can a GNU-Master shed some light?rinogo
Why do you need to have a "only hash" flag when you can trim the result with some regex? (I am sarcastic)Magnetic_dud

16 Answers

201
votes

Using AWK:

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

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
66
votes

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)
27
votes

On Mac OS X:

md5 -q file
16
votes
md5="$(md5sum "${my_iso_file}")"
md5="${md5%% *}" # remove the first space and everything after it
echo "${md5}"
8
votes

Another way is to do:

md5sum filename | cut -f 1 -d " "

cut will split the line to each space and return only the first field.

6
votes

One way:

set -- $(md5sum $file)
md5=$1

Another way:

md5=$(md5sum $file | while read sum file; do echo $sum; done)

Another way:

md5=$(set -- $(md5sum $file); echo $1)

(Do not try that with backticks unless you're very brave and very good with backslashes.)

The advantage of these solutions over other solutions is that they only invoke md5sum and the shell, rather than other programs such as awk or sed. Whether that actually matters is then a separate question; you'd probably be hard pressed to notice the difference.

3
votes
md5=$(md5sum < $file | tr -d ' -')
3
votes

If you need to print it and don't need a newline, you can use:

printf $(md5sum filename)
2
votes
md5=`md5sum ${my_iso_file} | cut -b-32`
1
votes

md5sum puts a backslash before the hash if there is a backslash in the file name. The first 32 characters or anything before the first space may not be a proper hash.

It will not happen when using standard input (file name will be just -), so pixelbeat's answer will work, but many others will require adding something like | tail -c 32.

0
votes

Well, I had the same problem today, but I was trying to get the file MD5 hash when running the find command.

I got the most voted question and wrapped it in a function called md5 to run in the find command. The mission for me was to calculate the hash for all files in a folder and output it as hash:filename.

md5() { md5sum $1 | awk '{ printf "%s",$1 }'; }
export -f md5
find -type f -exec bash -c 'md5 "$0"' {} \; -exec echo -n ':' \; -print

So, I'd got some pieces from here and also from 'find -exec' a shell function in Linux

0
votes

For the sake of completeness, a way with sed using a regular expression and a capture group:

md5=$(md5sum "${my_iso_file}" | sed -r 's:\\*([^ ]*).*:\1:')

The regular expression is capturing everything in a group until a space is reached. To get a capture group working, you need to capture everything in sed.

(More about sed and capture groups here: How can I output only captured groups with sed?)

As delimiter in sed, I use colons because they are not valid in file paths and I don't have to escape the slashes in the filepath.

0
votes

By leaning on head:

md5_for_file=`md5sum ${my_iso_file}|head -c 32`
-3
votes

Another way:

md5=$(md5sum ${my_iso_file} | sed '/ .*//' )
-3
votes
md5=$(md5sum < index.html | head -c -4)