248

I have a file file.gz, when I try to unzip this file by using gunzip file.gz, it unzipped the file but only contains extracted and removes the file.gz file.

How can I unzip by keeping both unzipped file and zipped file?

jack
  • 3,853
  • 11
  • 28
  • 33

2 Answers2

364

Here are several alternatives:

  • Give gunzip the --keep option (version 1.6 or later)

    -k   --keep
            Keep (don't delete) input files during compression or decompression.

    gunzip -k file.gz
    
  • Pass the file to gunzip as stdin

    gunzip < file.gz > file
    
  • Use zcat (or, on older systems, gzcat)

    zcat file.gz > file
    
Mark Plotnick
  • 24,913
  • 2
  • 59
  • 81
  • 7
    Where do I get a version of `gunzip` with `-k` or `--keep`? I'm using version 1.5 in Gentoo and it has no such option. – Pavel Šimerda Sep 18 '14 at 22:02
  • 2
    @PavelŠimerda It was added in version 1.6. I've amended my answer. Thanks. – Mark Plotnick Sep 18 '14 at 22:15
  • Thanks a lot! With the other option this is now the best answer. – Pavel Šimerda Sep 18 '14 at 22:21
  • 2
    @PavelŠimerda If you need `--keep` but don't have it, just make a copy of `file.gz` first. – Liam Aug 19 '15 at 16:33
  • @Liam That's an ugly hack given that you need to copy it to a third unique file name and then move. And the answer already has a *much* better solution. – Pavel Šimerda Aug 19 '15 at 19:15
  • 4
    `gunzip -c file.gz > file` or `zcat file.gz > file` also work. https://superuser.com/questions/45650/how-do-you-gunzip-a-file-and-keep-the-gz-file/45651 – phyatt May 01 '17 at 14:38
  • What if there are more files in the .gz? – Pyjong Mar 29 '19 at 15:27
  • @Pyjong gunzip or zcat of a file containing multiple compressed streams will produce the concatenation of all the streams. `cat thing1.gz thing2.gz > both.gz; gunzip -k both.gz` will produce a single file named `both` and won't delete `both.gz`. The gzip man page recommends that, if you need to compress multiple flles while preserving all their names, you use something like `tar` to bundle them up and give tar the `z` option to gzip the entire bundle. – Mark Plotnick Mar 29 '19 at 18:51
  • gunzip doesn't have a -k option. Is this answer still valid? `# gunzip -k my.gz gzip: invalid option -- 'k' Try 'gzip --help' for more information.` – Worp May 03 '19 at 06:59
  • @Worp What is the output of `gunzip --version` ? The -k option is only in version 1.6 and beyond. – Mark Plotnick May 03 '19 at 08:43
  • fml, that makes sense. `$ gunzip --version gunzip (gzip) 1.5` My bad, I will edit my comment! **edit:** I can't edit my comment anymore, so let's leave this up for clarification. Thanks for the info! – Worp May 03 '19 at 08:56
44

Without requiring a temporary file:

 zcat somefile.gz > somefile
LatinSuD
  • 1,401
  • 9
  • 8
  • 1
    This also works great if you want to pipe the output to another command with buffering without deleting the file `zcat file.sql.gz | mysql --max_allowed_packet=32M db` – Soheil Sep 19 '21 at 19:25