I need to use tar
in a pipeline inside a shell script to archive and compress some files.
After having read the manpage for tar
I was able to make tar
output to stdout
by running it along with the -O
argument, but I wasn't able to find out how to make it input from stdin
. Since many commands read from stdin
if no other input it's specified, I tried:
pv ~/foo | tar c -O ~/foo.tar
but that didn't work:
tar: Cowardly refusing to create an empty archive
Try 'tar --help' or 'tar --usage' for more information.
How can I make tar
read input from stdin
?
Using
-O
withc
is ignored, since no files are extracted. You'll get the same result either way:Which is why I find your error surprising, since
tar
is no coward if you specify a path.tar
cannot read in file data from stdin for creating an archive, since thentar
will have no way of knowing what a file is - where one begins or ends, what its path and metadata is.This Python script from my Unix.StackExchange answer appends stdin to a tar file, giving it an arbitrary name, using Python's tarfile library. It seeks back in the tar to rewrite the header with the right size at eof. The usage would be:
Here's the
tarappend
python script: