Ubuntu – How to make tar read input from stdin

inputtar

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?

Best Answer

Using -O with c is ignored, since no files are extracted. You'll get the same result either way:

$ tar c -O foo | tar t
foo
$ tar c foo | tar t  
foo

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 then tar will have no way of knowing what a file is - where one begins or ends, what its path and metadata is.

Related Question