How to gzip multiple files into one gz file?

77

21

I have 100 files: cvd1.txt, cvd2.txt ... cvd100.txt

How to gzip 100 files into one .gz file, so that after I gunzip it, I should have cvd1.txt, cvd2.txt ... cvd100.txt separately?

Tony

Posted 2011-03-29T01:22:26.120

Reputation: 883

Answers

42

if you have zip,

zip myzip.zip cvd*.txt

Don't need to tar them first.

kurumi

Posted 2011-03-29T01:22:26.120

Reputation:

You lose zgrep, zcmp, zdiff and all sorts of tools that can work on pipes by your choice of a non-streamable format called zip. Power users use pipes. – Tankman六四 – 2018-11-14T03:39:09.003

1@Kurumi- Do Windows programmes such as Winzip or 7-zip recognise .zip files? – None – 2011-03-29T01:45:55.867

1@Tony. I believe they do. I tested the 7-zip linux version with zip and able to extract. If you want, there is also GNU zip for windows. – None – 2011-03-29T02:06:09.413

85

You want to tar your files together and gzip the resulting tar file.

tar cvzf cvd.tar.gz cvd*.txt

To untar the gzip'd tar file you would do:

tar xvzf cvd.tar.gz -C /path/to/parent/dir

This would extract your files under the /path/to/parent/dir directory

SiegeX

Posted 2011-03-29T01:22:26.120

Reputation: 1 911

7if you name the file with the extension .tgz, (short for tar gz), then Windows programs will recognize it as something that winzip etc can process as is. Congrats to SiegeX for your 10K! – shellter – 2011-03-29T01:29:05.920

25

You'll want to use tar, like so:

tar -czvf file.tar.gz cvd*.txt

tar puts the files together, while gzip then performs the compression.

Quoth the gzip manpage:

If you wish to create a single archive file with multiple members so that members can later be extracted independently, use an archiver such as tar or zip. GNU tar supports the -z option to invoke gzip transparently. gzip is designed as a complement to tar, not as a replacement

Dan Fego

Posted 2011-03-29T01:22:26.120

Reputation: 371

13

gzip by itself does not know anything about file structure. To do what you want, you need to first put the files into some kind of container file (e.g. a tar structure, or similar) and then gzip that. tar has z and j (for bzip2) switches on GNU platforms to do this.

Joe

Posted 2011-03-29T01:22:26.120

Reputation: 694

2The switch is actually 'z' ('x' is for extract). Nice you mentioned 'j'/bzip2 - much tighter compression. – None – 2011-03-29T02:09:43.367

6

You can do it using:

zip my_final_filename.zip my_first_file my_second_file ... my_last_file

unzip my_final_filename.gz

or

tar cvzf my_final_filename.tar.gz my_first_file my_second_file ... my_last_file

tar -czvf my_final_filename.tar.gz

Unfortunately gzip is not capable of doing that. In case of more information please look at comments.

Vahid F

Posted 2011-03-29T01:22:26.120

Reputation: 69

I don't think your first command works. At least generally. If it works for a particular shell you should indicate it. I'd downvote if I had enough rep. – DPM – 2019-02-17T16:41:57.963

@DPM is right, the gzip/gunzip commands didn't work, it will return the error gzip: my_final_filename.gz: No such file or directory – Bilal – 2019-06-20T09:41:16.187

DPM and Bilal are right. Gzip is not capable of compressing multiple files into one. Look at this thread under Melebius answer: https://askubuntu.com/questions/1103018/gzip-2-files-into-one-file I edit my answer now.

– Vahid F – 2019-12-07T07:58:45.603

1

To compress multiple files with different patterns, we could this :

tar -czvf deploy.tar.gz **/Alice*.yml **/Bob*.json

this will add all .yml files that starts with Alice from any sub-directory and add all .json files that starts with Bob from any sub-directory.

Sairam Krish

Posted 2011-03-29T01:22:26.120

Reputation: 111

Already answered in https://superuser.com/a/334830/107419

– prayagupd – 2019-03-23T23:15:30.743