如何计算目录的 MD5 校验和? [英] How can I calculate an MD5 checksum of a directory?

查看:26
本文介绍了如何计算目录的 MD5 校验和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为放置在目录和所有子目录下的特定类型(例如 *.py)的所有文件计算汇总 MD5 校验和.

I need to calculate a summary MD5 checksum for all files of a particular type (*.py for example) placed under a directory and all sub-directories.

最好的方法是什么?

提议的解决方案非常好,但这并不是我所需要的.我正在寻找一种解决方案来获得一个单一摘要校验和,它将唯一地标识整个目录 - 包括其所有子目录的内容.

The proposed solutions are very nice, but this is not exactly what I need. I'm looking for a solution to get a single summary checksum which will uniquely identify the directory as a whole - including content of all its subdirectories.

推荐答案

find /path/to/dir/ -type f -name "*.py" -exec md5sum {} + | awk '{print $1}' | sort | md5sum

find 命令列出所有文件以 .py 结尾.为每个 .py 文件计算 MD5 哈希值.AWK 用于挑选 MD5 哈希值(忽略文件名,它可能不是唯一的).MD5 哈希值已排序.然后返回这个排序列表的 MD5 哈希值.

The find command lists all the files that end in .py. The MD5 hash value is computed for each .py file. AWK is used to pick off the MD5 hash values (ignoring the filenames, which may not be unique). The MD5 hash values are sorted. The MD5 hash value of this sorted list is then returned.

我已经通过复制测试目录对此进行了测试:

I've tested this by copying a test directory:

rsync -a ~/pybin/ ~/pybin2/

我重命名了 ~/pybin2 中的一些文件.

I renamed some of the files in ~/pybin2.

find...md5sum 命令为两个目录返回相同的输出.

The find...md5sum command returns the same output for both directories.

2bcf49a4d19ef9abd284311108d626f1  -

这篇关于如何计算目录的 MD5 校验和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆