用git bash调用md5sum进行批量MD5计算

2024-01-20 10:52

本文主要是介绍用git bash调用md5sum进行批量MD5计算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

对于非常大的文件或者很重要的文件,在不稳定的网络环境下,可能文件的某些字节会损坏。此时,对文件计算MD5即可以校验其完整性。比如本次的 OpenStreetMap 导出包,我的学弟反馈通过网盘下载无法解压,并建议我增加每个文件的MD5校验。

对于文件非常多的情况,需要批量计算。最简便的方法是使用git自带的md5sum进行计算。

1. 安装git并进入bash

到 https://git-scm.com/ 下载git,并安装。

安装后,右键单击网盘下载的文件夹,选择“git bash” 进入bash:

bash
bash
可以查看 md5sum的说明

$ md5sum --help
Usage: md5sum [OPTION]... [FILE]...
Print or check MD5 (128-bit) checksums.With no FILE, or when FILE is -, read standard input.-b, --binary         read in binary mode (default unless reading tty stdin)-c, --check          read MD5 sums from the FILEs and check them--tag            create a BSD-style checksum-t, --text           read in text mode (default if reading tty stdin)-z, --zero           end each output line with NUL, not newline,and disable file name escapingThe following five options are useful only when verifying checksums:--ignore-missing  don't fail or report status for missing files--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     display this help and exit--version  output version information and exitThe sums are computed as described in RFC 1321.  When checking, the input
should be a former output of this program.  The default mode is to print a
line with checksum, a space, a character indicating input mode ('*' for binary,
' ' for text or where binary is insignificant), and name for each FILE.Note: There is no difference between binary mode and text mode on GNU systems.GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/md5sum>
or available locally via: info '(coreutils) md5sum invocation'

2. 批量计算md5

Linux常见命令 find 能够枚举文件并批量执行指令。

执行下面的指令,可以在屏幕输出各个文件的md5:

$ find ./Arch*.* -exec md5sum {} \;
d060dd81785d957ae4e2bbd4f9ebeb4e *./ArchOSManjaro.7z.001
b7326e73452d3fbbc56a889f55aa9a14 *./ArchOSManjaro.7z.002
805c9ef68887953554c6c160c2a72eeb *./ArchOSManjaro.7z.003
#...
2cc5ab567abba1d7e3a284ec5c383d84 *./ArchOSManjaro.7z.059
$

执行下面的指令,可以在文件输出各个文件的md5:

$ find ./Arch*.* -exec md5sum {} >> md5.txt \;

3.比较两个文件是否一致

我们假设本地校验结果放在check.txt,标准校验结果放在 md5.txt,则使用下面指令比较:

$ diff --help
Usage: diff [OPTION]... FILES
Compare FILES line by line.Mandatory arguments to long options are mandatory for short options too.--normal                  output a normal diff (the default)-q, --brief                   report only when files differ-s, --report-identical-files  report when two files are the same-c, -C NUM, --context[=NUM]   output NUM (default 3) lines of copied context-u, -U NUM, --unified[=NUM]   output NUM (default 3) lines of unified context-e, --ed                      output an ed script-n, --rcs                     output an RCS format diff-y, --side-by-side            output in two columns-W, --width=NUM               output at most NUM (default 130) print columns--left-column             output only the left column of common lines--suppress-common-lines   do not output common lines-p, --show-c-function         show which C function each change is in-F, --show-function-line=RE   show the most recent line matching RE--label LABEL             use LABEL instead of file name and timestamp(can be repeated)-t, --expand-tabs             expand tabs to spaces in output-T, --initial-tab             make tabs line up by prepending a tab--tabsize=NUM             tab stops every NUM (default 8) print columns--suppress-blank-empty    suppress space or tab before empty output lines-l, --paginate                pass output through 'pr' to paginate it-r, --recursive                 recursively compare any subdirectories found--no-dereference            don't follow symbolic links-N, --new-file                  treat absent files as empty--unidirectional-new-file   treat absent first files as empty--ignore-file-name-case     ignore case when comparing file names--no-ignore-file-name-case  consider case when comparing file names-x, --exclude=PAT               exclude files that match PAT-X, --exclude-from=FILE         exclude files that match any pattern in FILE-S, --starting-file=FILE        start with FILE when comparing directories--from-file=FILE1           compare FILE1 to all operands;FILE1 can be a directory--to-file=FILE2             compare all operands to FILE2;FILE2 can be a directory-i, --ignore-case               ignore case differences in file contents-E, --ignore-tab-expansion      ignore changes due to tab expansion-Z, --ignore-trailing-space     ignore white space at line end-b, --ignore-space-change       ignore changes in the amount of white space-w, --ignore-all-space          ignore all white space-B, --ignore-blank-lines        ignore changes where lines are all blank-I, --ignore-matching-lines=RE  ignore changes where all lines match RE-a, --text                      treat all files as text--strip-trailing-cr         strip trailing carriage return on input--binary                    read and write data in binary mode-D, --ifdef=NAME                output merged file with '#ifdef NAME' diffs--GTYPE-group-format=GFMT   format GTYPE input groups with GFMT--line-format=LFMT          format all input lines with LFMT--LTYPE-line-format=LFMT    format LTYPE input lines with LFMTThese format options provide fine-grained control over the outputof diff, generalizing -D/--ifdef.LTYPE is 'old', 'new', or 'unchanged'.  GTYPE is LTYPE or 'changed'.GFMT (only) may contain:%<  lines from FILE1%>  lines from FILE2%=  lines common to FILE1 and FILE2%[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTERLETTERs are as follows for new group, lower case for old group:F  first line numberL  last line numberN  number of lines = L-F+1E  F-1M  L+1%(A=B?T:E)  if A equals B then T else ELFMT (only) may contain:%L  contents of line%l  contents of line, excluding any trailing newline%[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line numberBoth GFMT and LFMT may contain:%%  %%c'C'  the single character C%c'\OOO'  the character with octal code OOOC    the character C (other characters represent themselves)-d, --minimal            try hard to find a smaller set of changes--horizon-lines=NUM  keep NUM lines of the common prefix and suffix--speed-large-files  assume large files and many scattered small changes--color[=WHEN]       color output; WHEN is 'never', 'always', or 'auto';plain --color means --color='auto'--palette=PALETTE    the colors to use when --color is active; PALETTE isa colon-separated list of terminfo capabilities--help               display this help and exit-v, --version            output version information and exitFILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE' or 'FILE DIR'.
If --from-file or --to-file is given, there are no restrictions on FILE(s).
If a FILE is '-', read standard input.
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.Report bugs to: bug-diffutils@gnu.org
GNU diffutils home page: <https://www.gnu.org/software/diffutils/>
General help using GNU software: <https://www.gnu.org/gethelp/>

执行指令:

$ diff  check.txt  md5.txt
36c36
< 23dfa036cd5d772f01173da67ebf2634 *./ArchOSManjaro.7z.029
---
> db2ce0bc5c39fd5d8f672f478788095c *./ArchOSManjaro.7z.029

可以看到第29号文件有问题。

使用 -y 选项,可以查看完整输出(左右两列)

$ diff -y check.txt  md5.txt
..
e155774f7dd158ded02d9a9aae68f5eb *./ArchOSManjaro.7z.028        e155774f7dd158ded02d9a9aae68f5eb *./ArchOSManjaro.7z.028
23dfa036cd5d772f01173da67ebf2634 *./ArchOSManjaro.7z.029      | db2ce0bc5c39fd5d8f672f478788095c *./ArchOSManjaro.7z.029
c8c32363ebd14a7eefce1cadaaa64def *./ArchOSManjaro.7z.030        c8c32363ebd14a7eefce1cadaaa64def *./ArchOSManjaro.7z.030
...

不一致的行,会用竖线“|”标记。

这篇关于用git bash调用md5sum进行批量MD5计算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/625780

相关文章

IDEA中新建/切换Git分支的实现步骤

《IDEA中新建/切换Git分支的实现步骤》本文主要介绍了IDEA中新建/切换Git分支的实现步骤,通过菜单创建新分支并选择是否切换,创建后在Git详情或右键Checkout中切换分支,感兴趣的可以了... 前提:项目已被Git托管1、点击上方栏Git->NewBrancjsh...2、输入新的分支的

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

python如何调用java的jar包

《python如何调用java的jar包》这篇文章主要为大家详细介绍了python如何调用java的jar包,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录一、安装包二、使用步骤三、代码演示四、自己写一个jar包五、打包步骤六、方法补充一、安装包pip3 install

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Java调用C#动态库的三种方法详解

《Java调用C#动态库的三种方法详解》在这个多语言编程的时代,Java和C#就像两位才华横溢的舞者,各自在不同的舞台上展现着独特的魅力,然而,当它们携手合作时,又会碰撞出怎样绚丽的火花呢?今天,我们... 目录方法1:C++/CLI搭建桥梁——Java ↔ C# 的“翻译官”步骤1:创建C#类库(.NET