Unzipping a .gz file without removing the gzipped file

875,757

Solution 1

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
    

Solution 2

Without requiring a temporary file:

 zcat somefile.gz > somefile
Share:
875,757

Related videos on Youtube

Baraskar Sandeep
Author by

Baraskar Sandeep

Updated on September 18, 2022

Comments

  • Baraskar Sandeep
    Baraskar Sandeep over 1 year

    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?

  • Pavel Šimerda
    Pavel Šimerda over 9 years
    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.
  • Mark Plotnick
    Mark Plotnick over 9 years
    @PavelŠimerda It was added in version 1.6. I've amended my answer. Thanks.
  • Pavel Šimerda
    Pavel Šimerda over 9 years
    Thanks a lot! With the other option this is now the best answer.
  • Chaitanya Vella
    Chaitanya Vella over 8 years
    @PavelŠimerda If you need --keep but don't have it, just make a copy of file.gz first.
  • Pavel Šimerda
    Pavel Šimerda over 8 years
    @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.
  • phyatt
    phyatt about 7 years
    gunzip -c file.gz > file or zcat file.gz > file also work. superuser.com/questions/45650/…
  • Pyjong
    Pyjong about 5 years
    What if there are more files in the .gz?
  • Mark Plotnick
    Mark Plotnick about 5 years
    @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.
  • Worp
    Worp about 5 years
    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.
  • Mark Plotnick
    Mark Plotnick about 5 years
    @Worp What is the output of gunzip --version ? The -k option is only in version 1.6 and beyond.
  • Worp
    Worp about 5 years
    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!
  • Soheil
    Soheil over 2 years
    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