97

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?

BuZZ-dEE
  • 111
Tony
  • 1,093

6 Answers6

119

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
  • 2,467
58

if you have zip,

zip myzip.zip cvd*.txt

Don't need to tar them first.

36

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
  • 481
17

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
  • 914
7

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
  • 195
3

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.