Only get hash value using md5sum (without filename)

寵の児 提交于 2019-11-28 13:51:40

问题


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 ?


回答1:


Well another way :)

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



回答2:


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



回答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)



回答4:


On Mac OS X:

md5 -q file



回答5:


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



回答6:


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.




回答7:


Another way is to do :

md5sum filename |cut -f 1 -d " "

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




回答8:


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

printf $(md5sum filename)



回答9:


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



回答10:


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



回答11:


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.




回答12:


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?




回答13:


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.




回答14:


Another way:

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



回答15:


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


来源:https://stackoverflow.com/questions/3679296/only-get-hash-value-using-md5sum-without-filename

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!