How do you gunzip a file and keep the .gz file?

228

47

The default behavior of gunzip is to delete the .gz file after it decompresses.

How do I prevent it from deleting the file??

If this functionality is not included then is there an alternative program that allows this?

I'm using Ubuntu 9.04

Sen

Posted 2009-09-23T14:45:19.887

Reputation: 2 383

3use the following command: gunzip -k – fazineroso – 2015-08-03T13:45:20.953

what gunzip has -k option? I see no one. – Znik – 2015-11-27T14:42:14.230

1

@Znik, gzip/gunzip version 1.6 and higher has the option -k.

– aleksandr barakin – 2016-09-14T11:48:45.393

Answers

230

You're looking for:

gzcat x.txt.gz >x.txt

The gzcat command is equivalent to gunzip -c which simply writes the output stream to stdout. This will leave the compressed file untouched. So you can also use:

gunzip -c x.txt.gz >x.txt

Note that on some systems gzcat is also known as zcat so run like this instead:

zcat x.txt.gz >x.txt

user53528

Posted 2009-09-23T14:45:19.887

Reputation:

26it's probably just zcat on your system. – user23307 – 2010-01-31T14:52:08.593

1@schnaader looks like gunzip supports a --keep option as of version 1.6... – Jacob – 2014-10-02T12:53:22.077

Ah, thanks Jacob! @paxdiablo, could you edit the answer so it gives an example of the -k function? (http://savannah.gnu.org/forum/forum.php?forum_id=7623 , paragraph "New features")

– schnaader – 2014-10-06T07:41:57.450

This procedure doesn't work when begin of file is damaged. – Znik – 2015-11-27T14:40:41.803

2An additional option to keep the file would be nice indeed. For example, b(un)zip2 uses similar syntax and allows to simply add -k to keep the original file. – schnaader – 2011-09-08T20:19:40.063

1You can also add the line alias gzcat="gunzip -c" to your .bashrc – James Kingsbery – 2012-03-30T20:04:15.463

2Thanks for the answer. Growing up with pkunzip in DOS I find it so strange that there isn't an option to keep the file. Especially with a tool like gzip being so widely used... just weird. – Sen – 2009-09-23T14:59:47.040

4FYI, I actually don't have gzcat. Neither my local system or the system I was ssh'ed into. So, I have to use gunzip -c – Sen – 2009-09-23T16:15:18.627

@Sen: pkunzip is more directly comparable to tar -xzf <filename.tar.gz>; it extracts multiple files from a compressed file, and does not remove the compressed file. In *NIX systems, the concepts of "bundle multiple files together" and "compress data" aren't conflated like they are with .zip. This means that when new compression systems come along, we don't have to reinvent/reimplement the "bundle multiple files together" code/logic. – retracile – 2012-06-21T17:38:34.930

Let me clarify @JamesKingsbery where the line starts and ends alias gzcat="gunzip -c" ... Of course, I don't mind just using zcat. Less typing, more energy efficient ;) – Buttle Butkus – 2013-01-08T02:53:39.143

While this answer did help me, I still want to punish the guy who designed this command and decided that the destructive behavior should be the default.. – donquixote – 2014-03-18T03:28:29.247

56

You can use the -c option of gunzip which writes the output to stdout, and then pipe it to the file of your choice:

gunzip -c compressed-file.gz > decompressed-file

More details on the manual page.

Stef

Posted 2009-09-23T14:45:19.887

Reputation: 661

By the way, note that the man page referred to above lists a -k option, which means keep input files. Maybe this works on the BSD version, but it doesn't on mine, so the -c solution seems to be the correct one. – None – 2010-09-13T22:01:44.943

33

A simpler solution is to just use gunzip as a filter like this:

gunzip < myfile.gz > myfile

retracile

Posted 2009-09-23T14:45:19.887

Reputation: 2 586

I think this is the cleanest way since gunzip never knows what file it's getting, all it sees is a stream of data, so can't possibly alter the original file. – Walf – 2016-12-09T01:11:45.487

12

If it's actually a tarball (.tgz or .tar.gz extension), then instead of redirecting to file like all of the answers so far, you'll want to pipe it to tar, like so:

gunzip -c myfile.tar.gz | tar xvf -

so that you get the actual contents.

Alex

Posted 2009-09-23T14:45:19.887

Reputation: 1 848

7or just do: tar xzvf myfile.tar.gz – Joakim Elofsson – 2009-09-23T17:39:40.533

true, but only if you have GNU tar. The one that comes with Solaris, for example, doesn't support the z option. – Alex – 2009-09-23T20:01:40.603

5

gzip -dk myfile.gz

OR

gunzip -k myfile.gz

Comments:

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

Nabi K.A.Z.

Posted 2009-09-23T14:45:19.887

Reputation: 227

which version of gunzip are you using? gzip 1.5 from GNU has not the -k option – Rho Phi – 2020-02-03T12:01:05.160

gzip: invalid option -- 'k' – Putnik – 2020-02-06T09:44:02.720

@RhoPhi gzip and gunzip version: 1.10 , I use git bash (MINGW64) in windows – Nabi K.A.Z. – 2020-02-10T10:18:45.403

@Putnik I use 1.10 – Nabi K.A.Z. – 2020-02-10T10:18:58.030

5

Use the -c option to uncompress the file to stdout. It will not touch the original file.

gunzip -c myfile.gz > myfile

mob

Posted 2009-09-23T14:45:19.887

Reputation: 262

2

Gnu tar can read gzip files: tar -zxsvf myfile.tar.gz or tar -jxzvf myfile.tar.bz2 for bzipped tar files.

Justin Smith

Posted 2009-09-23T14:45:19.887

Reputation: 3 746