仅使用 md5sum 获取哈希值(不带文件名)
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
另一种方式:
Another way:
一个简单的数组赋值是有效的...请注意,Bash 数组的第一个元素可以仅通过
name
来寻址,而无需[0]
索引,即$md5
仅包含 md5sum 的 32 个字符。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.使用 AWK:
Using AWK:
您可以使用
cut
在空格上分割行并返回仅第一个这样的字段:You can use
cut
to split the line on spaces and return only the first such field:在 Mac OS X 上:
On Mac OS X:
另一种方法是:
cut 会将行分割到每个空格并仅返回第一个字段。
Another way is to do:
cut will split the line to each space and return only the first field.
依靠
头
:By leaning on
head
:如果您需要打印它并且不需要换行符,您可以使用:
If you need to print it and don't need a newline, you can use:
一种方式:
另一种方式:
另一种方式:(
不要尝试使用反斜杠,除非您非常勇敢并且非常擅长使用反斜杠。)
这些解决方案相对于其他解决方案的优势在于它们仅调用
md5sum
和 shell,而不是其他程序,例如awk
或sed
。这是否真的重要是一个单独的问题。您可能很难注意到其中的差异。One way:
Another way:
Another way:
(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 asawk
orsed
. Whether that actually matters is then a separate question; you'd probably be hard pressed to notice the difference.如果文件名中存在反斜杠,
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
.如果您担心奇怪的文件名:
其他更混乱的方法来处理这个问题:
完全在
或者 | awk '_{ 退出}++_' RS=' 'awk
内完成:完全在
awk
内完成:if you're concerned about screwy filenames :
other messier ways to deal with this :
to do it entirely inside
or | awk '_{ exit }++_' RS=' 'awk
:to do it entirely inside
awk
:好吧,我今天遇到了同样的问题,但我试图在运行
find
命令时获取文件 MD5 哈希值。我得到了投票最多的问题,并将其包装在一个名为
md5
的函数中,以便在find
命令中运行。我的任务是计算文件夹中所有文件的哈希值并将其输出为hash:filename
。所以,我从这里以及从 '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 thefind
command. The mission for me was to calculate the hash for all files in a folder and output it ashash:filename
.So, I'd got some pieces from here and also from 'find -exec' a shell function in Linux
为了完整起见,sed 使用正则表达式和捕获组的方式:
正则表达式捕获组中的所有内容,直到到达空格为止。要使捕获组正常工作,您需要捕获 sed 中的所有内容。
(有关 sed 和捕获组的更多信息,请参见:如何使用 sed 只输出捕获的组?)
作为 sed 中的分隔符,我使用冒号,因为它们在文件路径中无效,而且我不必转义文件路径中的斜杠。
For the sake of completeness, a way with sed using a regular expression and a capture group:
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.