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 stdingunzip < 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
Related videos on Youtube
Author by
Baraskar Sandeep
Updated on September 18, 2022Comments
-
Baraskar Sandeep over 1 year
I have a file
file.gz
, when I try to unzip this file by usinggunzip file.gz
, it unzipped the file but only contains extracted and removes thefile.gz
file.How can I unzip by keeping both unzipped file and zipped file?
-
Pavel Šimerda over 9 yearsWhere 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 over 9 years@PavelŠimerda It was added in version 1.6. I've amended my answer. Thanks.
-
Pavel Šimerda over 9 yearsThanks a lot! With the other option this is now the best answer.
-
Chaitanya Vella over 8 years@PavelŠimerda If you need
--keep
but don't have it, just make a copy offile.gz
first. -
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 about 7 years
-
Pyjong about 5 yearsWhat if there are more files in the .gz?
-
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 namedboth
and won't deleteboth.gz
. The gzip man page recommends that, if you need to compress multiple flles while preserving all their names, you use something liketar
to bundle them up and give tar thez
option to gzip the entire bundle. -
Worp about 5 yearsgunzip 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 about 5 years@Worp What is the output of
gunzip --version
? The -k option is only in version 1.6 and beyond. -
Worp about 5 yearsfml, 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 over 2 yearsThis 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