仅使用 md5sum 获取哈希值(不带文件名)

发布于 2024-09-18 01:59:34 字数 289 浏览 7 评论 0原文

我使用 md5sum 生成文件的哈希值。 但我只需要接收哈希值,而不是文件名。

md5=`md5sum ${my_iso_file}`
echo ${md5}

输出:

3abb17b66815bc7946cefe727737d295  ./iso/somefile.iso

如何“删除”文件名并仅保留值?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

高冷爸爸 2024-09-25 01:59:35
md5=$(md5sum < index.html | head -c -4)
md5=$(md5sum < index.html | head -c -4)
岁月如刀 2024-09-25 01:59:35

另一种方式:

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

Another way:

md5=$(md5sum ${my_iso_file} | sed '/ .*//' )
泪是无色的血 2024-09-25 01:59:34

一个简单的数组赋值是有效的...请注意,Bash 数组的第一个元素可以仅通过 name 来寻址,而无需 [0] 索引,即 $md5 仅包含 md5sum 的 32 个字符。

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

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
毁梦 2024-09-25 01:59:34

使用 AWK

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

Using AWK:

md5=`md5sum ${my_iso_file} | awk '{ print $1 }'`
挽你眉间 2024-09-25 01:59:34

您可以使用 cut 在空格上分割行并返回仅第一个这样的字段:

md5=$(md5sum "$my_iso_file" | cut -d ' ' -f 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)
萌面超妹 2024-09-25 01:59:34

在 Mac OS X 上:

md5 -q file

On Mac OS X:

md5 -q file
心房的律动 2024-09-25 01:59:34
md5="$(md5sum "${my_iso_file}")"
md5="${md5%% *}" # remove the first space and everything after it
echo "${md5}"
md5="$(md5sum "${my_iso_file}")"
md5="${md5%% *}" # remove the first space and everything after it
echo "${md5}"
青瓷清茶倾城歌 2024-09-25 01:59:34

另一种方法是:

md5sum filename | cut -f 1 -d " "

cut 会将行分割到每个空格并仅返回第一个字段。

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.

清旖 2024-09-25 01:59:34

依靠

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

By leaning on head:

md5_for_file=`md5sum ${my_iso_file}|head -c 32`
两仪 2024-09-25 01:59:34

如果您需要打印它并且不需要换行符,您可以使用:

printf $(md5sum filename)

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

printf $(md5sum filename)
情仇皆在手 2024-09-25 01:59:34

一种方式:

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

另一种方式:

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

另一种方式:(

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

不要尝试使用反斜杠,除非您非常勇敢并且非常擅长使用反斜杠。)

这些解决方案相对于其他解决方案的优势在于它们仅调用 md5sum和 shell,而不是其他程序,例如 awksed。这是否真的重要是一个单独的问题。您可能很难注意到其中的差异。

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.

再可℃爱ぅ一点好了 2024-09-25 01:59:34
md5=$(md5sum < $file | tr -d ' -')
md5=$(md5sum < $file | tr -d ' -')
陌伤浅笑 2024-09-25 01:59:34
md5=`md5sum ${my_iso_file} | cut -b-32`
md5=`md5sum ${my_iso_file} | cut -b-32`
寄风 2024-09-25 01:59:34

如果文件名中存在反斜杠,md5sum 会在哈希值之前添加一个反斜杠。前 32 个字符或第一个空格之前的任何内容可能不是正确的哈希。

使用标准输入时不会发生这种情况(文件名只是 -),因此 pixelbeat 的答案 会起作用,但许多其他答案将需要添加类似 | 的内容尾-c 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.

停滞 2024-09-25 01:59:34

如果您担心奇怪的文件名:

md5sum < “${文件名}”| awk NF=1   
f244e67ca3e71fff91cdf9b8bd3aa7a5

其他更混乱的方法来处理这个问题:

md5sum "${file_name}" | 
md5sum "${file_name}" | awk NF=NF OFS= FS=' .*

f244e67ca3e71fff91cdf9b8bd3aa7a5

完全在 awk 内完成:

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

    ( _=" md5sum < "((_)(__)_) ) |获取线

    打印 $(_*close(_)) }' "${file_name}"
f244e67ca3e71fff91cdf9b8bd3aa7a5
或者 | awk '_{ 退出}++_' RS=' '

完全在 awk 内完成:

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=' .*
f244e67ca3e71fff91cdf9b8bd3aa7a5

to do it entirely inside awk :

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

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

    print $(_*close(_)) }' "${file_name}"
f244e67ca3e71fff91cdf9b8bd3aa7a5
or | awk '_{ exit }++_' RS=' '

to do it entirely inside awk :

岁月如刀 2024-09-25 01:59:34

好吧,我今天遇到了同样的问题,但我试图在运行 find 命令时获取文件 MD5 哈希值。

我得到了投票最多的问题,并将其包装在一个名为 md5 的函数中,以便在 find 命令中运行。我的任务是计算文件夹中所有文件的哈希值并将其输出为 hash:filename

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

所以,我从这里以及从 'find -exec' Linux 中的 shell 函数

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

林空鹿饮溪 2024-09-25 01:59:34

为了完整起见,sed 使用正则表达式和捕获组的方式:

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

正则表达式捕获组中的所有内容,直到到达空格为止。要使捕获组正常工作,您需要捕获 sed 中的所有内容。

(有关 sed 和捕获组的更多信息,请参见:如何使用 sed 只输出捕获的组?

作为 sed 中的分隔符,我使用冒号,因为它们在文件路径中无效,而且我不必转义文件路径中的斜杠。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文