Linux操作系统:计算一个给定的文件夹及放一个散列;内容? [英] Linux: compute a single hash for a given folder & contents?

查看:114
本文介绍了Linux操作系统:计算一个给定的文件夹及放一个散列;内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然,必须有一个方法可以轻松地做到这一点!

Surely there must be a way to do this easily!

我试过在Linux命令行应用的sha1sum和放大器;的md5sum但他们似乎只能够计算各个文件和输出的散列每个文件一个散列值的列表。

I've tried the linux command-line apps sha1sum & md5sum but they seem only to be able to compute hashes of individual files and output a list of hash values, one for each file.

我需要生成的文件夹中的全部内容(不只是文件名)的单个散列

I need to generate a single hash for the entire contents of a folder (not just the filenames).

我要像做

sha1sum /folder/of/stuff > singlehashvalue

编辑:,以澄清,我的文件在目录树中多层次的,他们不是都坐在同根文件夹

to clarify, my files are at multiple levels in a directory tree, they're not all sitting in the same root folder.

推荐答案

一种可能的方式是:


sha1sum path/to/folder/* | sha1sum

如果有一整个目录树,你可能会更好过使用find和xargs的。一个可能的命令是

If there is a whole directory tree, you're probably better off using find and xargs. One possible command would be


find path/to/folder -type f -print0 | xargs -0 sha1sum | sha1sum

修改:好点,这可能是一件好事来排序文件列表,所以:

Edit: Good point, it's probably a good thing to sort the list of files, so:


find path/to/folder -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum

最后,如果您还需要考虑到的权限和空目录:

And, finally, if you also need to take account of permissions and empty directories:

(find path/to/folder -type f -print0  | sort -z | xargs -0 sha1sum;
 find path/to/folder \( -type f -o -type d \) -print0 | sort -z | \
   xargs -0 stat -c '%n %a') \
| sha1sum

的参数与stat 将导致它来打印文件的名称,然后是它的八进制权限。这两个发现将运行一前一后,引起磁盘IO,第一发现所有文件名的双倍数量和执行校验的内容,该第二查找所有文件和目录名,印刷的名称和模式。的文件名和校验,其次是名和目录,具有权限列表将被校验和,一个较小的校验和。

The arguments to stat will cause it to print the name of the file, followed by its octal permissions. The two finds will run one after the other, causing double the amount of disk IO, the first finding all file names and checksumming the contents, the second finding all file and directory names, printing name and mode. The list of "file names and checksums", followed by "names and directories, with permissions" will then be checksummed, for a smaller checksum.

这篇关于Linux操作系统:计算一个给定的文件夹及放一个散列;内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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