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.

2

1 Answer 1

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 
3
  • 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... Commented Nov 19, 2021 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 ``` Commented Nov 20, 2021 at 0: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". Commented Nov 20, 2021 at 10:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.