Only get hash value using md5sum (without filename)
BashShellMd5sumBash Problem Overview
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?
Bash Solutions
Solution 1 - Bash
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 - Bash
Using AWK:
md5=`md5sum ${my_iso_file} | awk '{ print $1 }'`
Solution 3 - Bash
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 - Bash
On Mac OS X:
md5 -q file
Solution 5 - Bash
md5="$(md5sum "${my_iso_file}")"
md5="${md5%% *}" # remove the first space and everything after it
echo "${md5}"
Solution 6 - Bash
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.
Solution 7 - Bash
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.
Solution 8 - Bash
md5=$(md5sum < $file | tr -d ' -')
Solution 9 - Bash
If you need to print it and don't need a newline, you can use:
printf $(md5sum filename)
Solution 10 - Bash
md5=`md5sum ${my_iso_file} | cut -b-32`
Solution 11 - Bash
By leaning on head
:
md5_for_file=`md5sum ${my_iso_file}|head -c 32`
Solution 12 - Bash
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
.
Solution 13 - Bash
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 https://stackoverflow.com/questions/4321456/find-exec-a-shell-function-in-linux
Solution 14 - Bash
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: https://stackoverflow.com/questions/2777579/how-can-i-output-only-captured-groups-with-sed/2778096#2778096)
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.
Solution 15 - Bash
Another way:
md5=$(md5sum ${my_iso_file} | sed '/ .*//' )
Solution 16 - Bash
md5=$(md5sum < index.html | head -c -4)