Can I calculate the checksum of a file as I create it?

4

I am creating a large tar archive and I would like to create the checksum of the archive too. I could achieve it like this:

$ tar cfz archive.tar.gz files
$ sha256sum archive.tar.gz > archive.tar.gz.sha256sum

But the archive file is huge and on slow media, so I'd prefer not to have to read it all in again after writing it out.

Can I build a pipeline that will hash the file as it writes it? I thought maybe I could do this with the tee utility, but that only writes to a file, not to the standard input of another command.

jl6

Posted 2016-08-11T21:36:50.720

Reputation: 1 025

Answers

5

Answering my own question:

Yes, you can use tee and bash process substitution:

tar cfz - files | tee >(sha256sum) | cat > archive.tar.gz

jl6

Posted 2016-08-11T21:36:50.720

Reputation: 1 025

1Minor improvements: You probably don't need the "| cat" part, you can just write it directly. You also probably want the subcommand (the "(sha256sum)" to write somewhere, i.e. (sha256sum >archive.tar.gz.sha256sum) – MAP – 2016-08-12T04:51:57.647

3Just use | tee archive.tar.gz | sha256sum then. – user1686 – 2016-08-12T07:24:43.303