Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can multiple .gz files be combined such that they extract into a single file? [duplicate]

Tags:

linux

unix

gzip

Lets say that I have 3 file: 1.txt, 2.txt and 3.txt which have all be gzipped. I'm aware that gzip allows multiple files to be combined using cat:

cat 1.gz 2.gz 3.gz > 123.gz

However when 123.gz is extracted it will produce the original 3 files.

Is it possible to combine the three archives in a way that the individual files within the archive will be combined into a single file as well?

like image 850
slayton Avatar asked May 23 '13 13:05

slayton


People also ask

Can you concatenate .gz files?

Concatenation of Gzip Files We can use some common commands like cat and tar to concatenate Gzip files in the Linux system.

Can gz contain multiple files?

A GZ file is a compressed single file, as the Gzip compression utility only compresses one file. If you compress multiple files with the Gzip compression utility, it produces multiple GZ files. The Gzip compression utility is common in Linux/Unix operating systems.

How unzip multiple gz file in Linux?

how can I extract multiple gzip files in directory and subdirectories? It will extract all files with their original names and store them in the current user home directory( /home/username ). gunzip *. gz // This command also will work.


1 Answers

Surprisingly, this is actually possible.

The GNU zip man page states: multiple compressed files can be concatenated. In this case, gunzip will extract all members at once.

Example:

You can build the zip like this:

echo 1 > 1.txt ; echo 2 > 2.txt; echo 3 > 3.txt;
gzip 1.txt; gzip 2.txt; gzip 3.txt;
cat 1.txt.gz 2.txt.gz 3.txt.gz > all.gz

Then extract it:

gunzip -c all.gz > all.txt

The contents of all.txt should now be:

1
2
3

Which is the same as:

cat 1.txt 2.txt 3.txt

And - as you requested - "gunzip will extract all members at once".

like image 124
djf Avatar answered Sep 25 '22 09:09

djf