up vote 20 down vote favorite
3
share [g+] share [fb]

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

link|improve this question
feedback

migrated from stackoverflow.com Sep 23 '09 at 14:54

This question came from our site for professional and enthusiast programmers.

protected by studiohack Apr 27 '11 at 0:57

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

6 Answers

up vote 22 down vote accepted

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
link|improve this answer
Thanks 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 Sep 23 '09 at 14:59
2  
FYI, 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 Sep 23 '09 at 16:15
3  
it's probably just zcat on your system. – user23307 Jan 31 '10 at 14:52
An 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 Sep 8 '11 at 20:19
feedback

You can the -c option 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.

link|improve this answer
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. – user49260 Sep 13 '10 at 22:01
feedback

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

gunzip -c myfile.gz > myfile
link|improve this answer
feedback

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.

link|improve this answer
4  
or just do: tar xzvf myfile.tar.gz – Joakim Elofsson Sep 23 '09 at 17:39
true, but only if you have GNU tar. The one that comes with Solaris, for example, doesn't support the z option. – Alex Sep 23 '09 at 20:01
feedback

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

gunzip < myfile.gz > myfile
link|improve this answer
feedback

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

link|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.