开发者

Only get hash value using md5sum (without filename)

开发者 https://www.devze.com 2023-01-16 01:52 出处:网络
I use md5sum to generate a hash value for a file. But I only need to receive the hash value, not the file name.

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 fi开发者_如何学编程le name and only retain the value?


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


Using AWK:

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


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}"


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.


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.


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

printf $(md5sum filename)


By leaning on head:

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


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


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


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.


if you're concerned about screwy filenames :

md5sum < "${file_name}" | awk NF=1   
f244e67ca3e71fff91cdf9b8bd3aa7a5

other messier ways to deal with this :

md5sum "${file_name}" | awk NF=NF OFS= FS=' .*$' 
                     or
                      | awk '_{ exit }++_' RS=' '    
f244e67ca3e71fff91cdf9b8bd3aa7a5

to do it entirely inside awk :

mawk 'BEGIN {
    __ = ARGV[ --ARGC ]
     _ = sprintf("%c",(_+=(_^=_<_)+_)^_+_*++_) 
    RS = FS
    gsub(_,"&\\\\&",__)

    ( _=" md5sum < "((_)(__)_) ) | getline

    print $(_*close(_)) }' "${file_name}"
f244e67ca3e71fff91cdf9b8bd3aa7a5


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


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.


Another way:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号