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}

3abb17b66815bc7946cefe727737d295 ./iso/somefile.iso

How can i 'strip' the file name and only remain the value ?

Answers


Well another way :)

md5=`md5sum ${my_iso_file} | awk '{ print $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, ie, $md5 contains only the 32 chars of the md5sum.

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

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)

On Mac OS X:

md5 -q file

md5="$(md5sum "${my_iso_file}")"
md5="${md5%% *}" # remove the first space and everything after it
echo "${md5}"

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 back-ticks 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.


Another way is to do :

md5sum filename |cut -f 1 -d " "

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


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

printf $(md5sum filename)

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

md5=`md5sum ${my_iso_file} | cut -b-32`

md5sum puts backslash before hash if there is backslash in file name. First 32 characters or anything before 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.


Well, I had the same problem today, but trying to get 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 find command. The mission for me was calculate hash for all files in an 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?


For the sake of completeness a way with sed using regex and capture group:

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

The regulare expression is capturing everything in a group until a space is reached. To get capture group working you need to capture everything in sed. (More about sed and caputer groups here: https://stackoverflow.com/a/2778096/10926293) As delimiter in sed i use colons because they are not valid in file paths and i don't have to escape the slashed in the filepath.


Another way:

md5=$(md5sum ${my_iso_file} | sed '/ .*//' )

md5=$(md5sum < index.html | head -c -4)

Need Your Help

How to pass parameters to a modal?

javascript twitter-bootstrap grails angularjs

I want to pass the userName from a list of userNames a logged in user clicks on to twitter bootstrap modal.

SELECT where row value contains string MySQL

php mysql

How can I select a row in my MySQL DB where the value of a column contains 'XcodeDev' for example?