Linux:计算给定文件夹的单个散列 &内容? [英] Linux: compute a single hash for a given folder & contents?

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

问题描述

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

我尝试过 Linux 命令行应用程序,例如 sha1summd5sum 但它们似乎只能计算单个文件的哈希值并输出一个列表哈希值,每个文件一个.

我需要为文件夹的全部内容(不仅仅是文件名)生成一个哈希值.

我想做类似的事情

sha1sum/folder/of/stuff >单哈希值

澄清一下,我的文件位于目录树中的多个级别,它们并不都位于同一个根文件夹中.

解决方案

一种可能的方法是:

sha1sum 路径/到/文件夹/* |sha1sum

如果有一个完整的目录树,最好使用 find 和 xargs.一种可能的命令是

<前>find path/to/folder -type f -print0 |排序 -z |xargs -0 sha1sum |sha1sum

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

(find path/to/folder -type f -print0 | sort -z | xargs -0 sha1sum;找到路径/到/文件夹 ( -type f -o -type d ) -print0 |排序 -z |xargs -0 stat -c '%n %a') |sha1sum

stat 的参数将导致它打印文件的名称,然后是它的八进制权限.两次查找将依次运行,导致磁盘IO量翻倍,第一次查找所有文件名并校验内容,第二次查找所有文件和目录名,打印名称和模式.然后将对文件名和校验和"列表,后跟具有权限的名称和目录"列表进行校验和,以获得较小的校验和.

Surely there must be a way to do this easily!

I've tried the Linux command-line apps such as sha1sum and 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).

I'd like to do something like

sha1sum /folder/of/stuff > singlehashvalue

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

解决方案

One possible way would be:

sha1sum path/to/folder/* | sha1sum

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 | 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

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:计算给定文件夹的单个散列 &amp;内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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