Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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
show 1 more comment

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
1  
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
add comment

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

share|improve this answer
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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