当前位置: 代码迷 >> 综合 >> Linux的md5sum命令用法
  详细解决方案

Linux的md5sum命令用法

热度:14   发布时间:2023-10-24 23:30:59.0

MD5算法可以用来验证网络文件传输的完整性。Linux的md5sum命令便用于生成和校验文件的md5值。需要注意的是,md5sum检验的是文件内容,而不是文件名。        

语法:

md5sum [选项]... [文件]...

选项:

-b, --binary          以二进制模式读取-c, --check           从文件中读取MD5 的校验值并予以检查--tag            create a BSD-style checksum-t, --text            以纯文本模式读取(默认)Note: There is no difference between binary and text mode option on GNU system.The following four options are useful only when verifying checksums:--quiet          don't print OK for each successfully verified file--status         don't output anything, status code shows success--strict         exit non-zero for improperly formatted checksum lines-w, --warn           warn about improperly formatted checksum lines--help            显示此帮助信息并退出--version         显示版本信息并退出

常见用法:

(1)查看字符串的md5值

[root@k8s-m1 ~]# echo "apple" | md5sum
30c6677b833454ad2df762d3c98d2409  -
[root@k8s-m1 ~]#
[root@k8s-m1 ~]# echo "apple" | md5sum | awk '{ print $1 }'
30c6677b833454ad2df762d3c98d2409

(2)查看文件的md5值

[root@k8s-m1 ~]# echo "hello" > testmd5
[root@k8s-m1 ~]#
[root@k8s-m1 ~]# md5sum testmd5
b1946ac92492d2347c6235b4d2611184  testmd5

(3)注意:md5检验的是文件的内容,与文件名无关

[root@k8s-m1 ~]# echo "banana" > test1
[root@k8s-m1 ~]#
[root@k8s-m1 ~]# echo "banana" > test2
[root@k8s-m1 ~]#
[root@k8s-m1 ~]# md5sum test1 | awk '{ print $1 }'
df3e129a722a865cc3539b4e69507bad
[root@k8s-m1 ~]#
[root@k8s-m1 ~]# md5sum test2 | awk '{ print $1 }'
df3e129a722a865cc3539b4e69507bad

以上可见test1和test2两个文件内容一致。