1

Using tar, is it possible to compress stdin text and have the compressed data appear in stdout? Example:

$ echo "test" | tar c - 
<compressed blob here>

I'm using macOS Mojave.

marked as duplicate by filbranden, Kusalananda linux Nov 22 '18 at 18:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

In short, no it's not possible to do that without storing the whole contents of stdin into a file or temporary file (e.g. in memory.)

This is a limitation of the tar file format itself, since before storing the file contents, tar stores a descriptor for the file, including file name, etc. and also the file size. So tar needs to know what is the size of the file before storing it. When reading from stdin, tar can't know the size of the contents, unless it reads all of them, in which case it'll need to store them somewhere, in order to store those contents in the tarball itself.

See also "Is it possible to make a .tar.gz file directly from stdin? Or, I need to tar together already gzipped files." One of the answers lists a way to store the contents of the file first, then seek back and update the file descriptor to write the actual size of the contents. But that requires the ability to seek on the output file, which is typically not the case if you're writing to stdout...

In short, you need to write your input to a temporary file, in order to add it to a tarball.

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