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.


Answering my own question:

Yes, you can use tee and bash process substitution:

tar cfz - files | tee >(sha256sum) | cat > archive.tar.gz
  • Minor 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 Aug 12 '16 at 4:51
  • 2
    Just use | tee archive.tar.gz | sha256sum then. – grawity Aug 12 '16 at 7:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.