Howto pipe: cp | tar | gzip without creating intermediary files?

32

11

Can anyone tell me if it's possible to pipe | this without having to create a physical file anywhere between A and B.tar.gz?

This is what I'm trying to do:

  1. File A
  2. Rename A to B
  3. Tar B.tar
  4. gzip -9 B.tar.gz

So for example:

cp A B | tar cvf - B | gzip -9 B.tar.gz

regan

Posted 2011-08-05T11:27:14.683

Reputation:

why would you like to pipe cp A B ? – Dor – 2011-08-05T11:30:22.870

1rename is mv. – Karoly Horvath – 2011-08-05T11:44:45.837

3Please explain why you would like to copy A to B first. – speakr – 2012-06-21T10:23:34.680

Answers

45

The following examples can be used to avoid creating intermediary files:

tar with gzip:

tar cf - A | gzip -9 > B.tar.gz

gzip without tar:

gzip -9c A > B.gz

tar without gzip:

tar cf B.tar A

Renaming (moving) A to B first doesn't make sense to me. If this is intended, then just put a mv A B && before either of the above commands and exchange A with B there.

Example with tar and gzip:

mv A B && tar cf - B | gzip -9 > B.tar.gz

speakr

Posted 2011-08-05T11:27:14.683

Reputation: 3 379

1This is correct and should be marked as such. You must include a "-" after "tar" for the piped option otherwise you get "tar: Cowardly refusing to create an empty archive". – Adambean – 2017-07-22T10:38:11.817

7

It depends on your version of tar

If you have the version that supports member transforms (--transform or --xform) then you can simply do

tar -c --transform=s/A/B/ A | gzip -9 > B.tar.gz

the | gzip -9 >B.tar.gz can be avoided if your tar supports the -z option

tar -zcvf B.tar.gz --transform=s/A/B/ A

If your version of tar doesn't support --transform then you will have to just copy the file first eg

 cp A B && tar -zcvf B.tar.gz B

However if you are only compressing one file why not skip the tar part all together and just do

cat A | gzip -9 > B.gz

Bob Vale

Posted 2011-08-05T11:27:14.683

Reputation: 271

I've used cp as you did in your example but rename is mv – None – 2011-08-05T11:45:39.817

0

If you are using cp to make a copy with a different name/location, then just include the full/final pathname you want when creating your completed .gzip file:

tar -cvf existing.file | gzip -1 > ./newdirectory/newnamed.file.tgz

Guest

Posted 2011-08-05T11:27:14.683

Reputation: 1

This does not work, produces "tar: Cowardly refusing to create an empty archive". – Adambean – 2017-07-22T10:37:04.103

@Adambean Maybe you want to use tar -czf - some-folder > some-archive.tar.gz? – tsauerwein – 2018-09-05T10:57:00.807

1@tsauerwein, yes, see my reply to the below answer from 14 months ago... – Adambean – 2018-09-05T16:23:51.710