Shell-Script

OSX:在包含具有相應校驗和的文件的文本文件中遞歸生成 MD5 校驗和

  • October 3, 2017

在具有多個子目錄但只有一個包含 tiff 文件的文件夾深的目錄中,我想生成一個 md5 校驗和,將具有相應校驗和的文件名寫入文本文件。

例如在目錄 TIFF 我有 2 個子目錄:

TIFF
 |- b0125TIFF
       |- b_0000_001.tif
       |- b_0000_002.tif
       |- b_0000_003.tif
       |- b_0000_004.tif
 |- c0126TIFF
       |- c_0000_001.tif
       |- c_0000_002.tif
       |- c_0000_003.tif
       |- c_0000_004.tif

我預期的文本文件(校驗和當然應該不同):

** foo.md5:
188be1dbd4f6bcfdef8d25639473e6ec *b0125TIFF/b_0000_001.tif
188be1dbd4f6bcfdef8d25639473e6ec *b0125TIFF/b_0000_002.tif
188be1dbd4f6bcfdef8d25639473e6ec *b0125TIFF/b_0000_003.tif
188be1dbd4f6bcfdef8d25639473e6ec *b0125TIFF/b_0000_004.tif
188be1dbd4f6bcfdef8d25639473e6ec *c0126TIFF/c_0000_001.tif
188be1dbd4f6bcfdef8d25639473e6ec *c0126TIFF/c_0000_002.tif
188be1dbd4f6bcfdef8d25639473e6ec *c0126TIFF/c_0000_003.tif
188be1dbd4f6bcfdef8d25639473e6ec *c0126TIFF/c_0000_004.tif

我怎樣才能做到這一點?

我知道這會在一個目錄中遞歸生成校驗和:

find -s . -type f -exec md5 -q {} \; | md5

你不想傳遞findand md5through的輸出md5,那隻會給你一個很多 MD5 校驗和的 MD5 校驗和……


$ find TIFF -type f -name '*.tif' -exec md5 {} ';' >md5.txt
$ cat md5.txt
MD5 (TIFF/b0125TIFF/file-1.tif) = d41d8cd98f00b204e9800998ecf8427e
MD5 (TIFF/b0125TIFF/file-2.tif) = d41d8cd98f00b204e9800998ecf8427e
MD5 (TIFF/b0125TIFF/file-3.tif) = d41d8cd98f00b204e9800998ecf8427e
MD5 (TIFF/c0126TIFF/file-1.tif) = d41d8cd98f00b204e9800998ecf8427e
MD5 (TIFF/c0126TIFF/file-2.tif) = d41d8cd98f00b204e9800998ecf8427e
MD5 (TIFF/c0126TIFF/file-3.tif) = d41d8cd98f00b204e9800998ecf8427e

不幸的是, macOS 上的md5實現不支持驗證校驗和,但該實用程序支持:md5 -c``shasum

$ find TIFF -type f -name '*.tif' -exec shasum {} ';' >sums.txt
$ cat sums.txt
da39a3ee5e6b4b0d3255bfef95601890afd80709  TIFF/b0125TIFF/file-1.tif
da39a3ee5e6b4b0d3255bfef95601890afd80709  TIFF/b0125TIFF/file-2.tif
da39a3ee5e6b4b0d3255bfef95601890afd80709  TIFF/b0125TIFF/file-3.tif
da39a3ee5e6b4b0d3255bfef95601890afd80709  TIFF/c0126TIFF/file-1.tif
da39a3ee5e6b4b0d3255bfef95601890afd80709  TIFF/c0126TIFF/file-2.tif
da39a3ee5e6b4b0d3255bfef95601890afd80709  TIFF/c0126TIFF/file-3.tif

$ shasum -c sums.txt
TIFF/b0125TIFF/file-1.tif: OK
TIFF/b0125TIFF/file-2.tif: OK
TIFF/b0125TIFF/file-3.tif: OK
TIFF/c0126TIFF/file-1.tif: OK
TIFF/c0126TIFF/file-2.tif: OK
TIFF/c0126TIFF/file-3.tif: OK

shasum預設情況下計算文件的 SHA1 雜湊。

引用自:https://unix.stackexchange.com/questions/395778