4

I have 2 gz files those I need to merge into one -- that is to say I want to combine two .gz files into one such that when I extract the combined file I get a single file containing the concatenation of the original input files.

time join <(zcat r_TR2_2012-05-28-08-10-00.gz) <(zcat r_TR1_2012-05-28-08-10-00.gz)

The above statement is not working as expected. I am using 3 commands to do the needful.

gunzip r_TR2_2012-05-28-08-10-00.gz 
gunzip r_TR1_2012-05-28-08-10-00.gz 
tar -zcvf combined.tar.gz r_TR1_2012-05-28-08-10-00 r_TR2_2012-05-28-08-10-00 

and then concatenating the files together when I extract them to produce the output I want.

Is there any way to do this in 1 statement?

voretaq7
  • 79,879
  • 17
  • 130
  • 214
shantanuo
  • 3,579
  • 8
  • 49
  • 66
  • When the final file extracted, do you want two files extracted, or a single file which is the concatenation of the two files? – mgorven Jun 27 '12 at 04:48
  • single gz file. – shantanuo Jun 27 '12 at 05:45
  • That doesn't answer my question. When the final compressed file is extracted, do you want `r_TR1_2012-05-28-08-10-00` and `r_TR2_2012-05-28-08-10-00`, or do you want a single uncompressed file which is those two files concatenated together? – mgorven Jun 27 '12 at 06:10
  • 2
    How about this `cat r_TR1_2012-05-28-08-10-00.gz r_TR2_2012-05-28-08-10-00.gz > final.gz`? – quanta Jun 27 '12 at 07:10
  • final.gz will have 2 files in it. I need single uncompressed file that is concatenation of those two files. – shantanuo Jun 28 '12 at 04:18
  • @shantanuo I have tried to clarify your wording in your question based on your comments. If I've botched it (or you can think of a clearer way to describe what you want) please fix as appropriate. – voretaq7 Jul 27 '12 at 20:39

3 Answers3

7

I guess you mean:

zcat r_TR2_2012-05-28-08-10-00.gz r_TR1_2012-05-28-08-10-00.gz| gzip -9 > joined.gz

Bgs
  • 208
  • 2
  • 5
5

Surprisingly what Quanta posted in the comments works -- at least on my Mac!

$ echo abc > foo
$ echo cba > bar
$ gzip foo
$ gzip bar
$ cat foo.gz bar.gz > baz.gz
$ gunzip baz.gz 
$ cat baz 
abc
cba
$

I am somewhat disturbed, yet also impressed.

voretaq7
  • 79,879
  • 17
  • 130
  • 214
2

If you have multiple compressed files .gz and you need to combine them into one file, you can use tar directly to do so. No need to uncompress and then compress them again.

You can try this:

tar cvf combined.gz.tar r_TR1_2012-05-28-08-10-00.gz r_TR2_2012-05-28-08-10-00.gz

However, you will get a file with extension .gz.tar as opposed to the known .tar.gz. To uncompress the file, you can try:

tar -xavf combined.gz.tar

The option -a is useful here (from man tar):

-a, --auto-compress
       use archive suffix to determine the compression program
Khaled
  • 36,533
  • 8
  • 72
  • 99