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?

  • 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 4:48
  • single gz file. – shantanuo Jun 27 '12 at 5: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 6: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 7: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 4:18

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 

I am somewhat disturbed, yet also impressed.

| improve this answer | |
  • 2
    Yup, it's intentional. – mgorven Jul 27 '12 at 21:07
  • I find such behavior disturbing... but I guess someone had the same use case as @shantanuo – voretaq7 Jul 27 '12 at 21:30

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

| improve this answer | |

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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