0

I am on a 2015 MacBook Pro and have just updated to OS 12.01 Monterey, and to zsh on terminal.

I am able to create a checksum for individual files using shasum -a 256 (then drag in a file).

However, I need to know how to create a checksum for a folder.

Then I need to know how to create a compare checksum script or command.

AdminBee
  • 22,803

1 Answers1

0

Here's what I would start with, using "scratch" as the source and "zork" as the new copy (which could be on a different dist). Note that I'm using the -a flag to cp so that file timestamps are preserved during copy.

% (cd scratch ; tar cf - .) | shasum -a 256
a17cacd171d6cbc2f6da028c8167b0602a1146a337f602de71529999fe471e0f  -

% /bin/cp -a scratch zork

% (cd zork ; tar cf - .) | shasum -a 256 a17cacd171d6cbc2f6da028c8167b0602a1146a337f602de71529999fe471e0f -

If you want to compare the two sums directly, you could use

if [[ $((cd scratch ; tar cf - .) | shasum -a 256) == $((cd zork ; tar cf - .) | shasum -a 256) ]]
then
  echo match
else
  echo no match
fi
smolin
  • 31
  • This is fragile; the archive generated by tar will depend on how the filesystem orders files in directory listings, on how accurately cp copies holes in sparse files, in the case of macOS probably even on how the filesystem normalizes Unicode... – u1686_grawity Nov 19 '21 at 20:10
  • Interesting points @user1666 would it help to use find instead?
    find scratch/ -type f -exec shasum -a 256 {} + | cut -d' ' -f 1 | shasum -a 256
    
    – smolin Nov 20 '21 at 00:25
  • If you sorted the list of per-file shasums (though there's probably no need to throw away file names), then yes, otherwise it still depends on the "filesystem order". – u1686_grawity Nov 20 '21 at 10:39