282

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
  • 2,923
  • 2
  • 16
  • 5

7 Answers7

291

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
rogerdpack
  • 2,146
  • 7
  • 32
  • 49
  • 2
    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
  • 4
    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
  • 31
    it's probably just zcat on your system. – user23307 Jan 31 '10 at 14:52
  • 2
    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 08 '11 at 20:19
  • 1
    You can also add the line alias gzcat="gunzip -c" to your .bashrc – James Kingsbery Mar 30 '12 at 20:04
  • @Sen: pkunzip is more directly comparable to tar -xzf ; 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 Jun 21 '12 at 17:38
  • 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 Jan 08 '13 at 02:53
  • 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 Mar 18 '14 at 03:28
  • 1
    @schnaader looks like gunzip supports a --keep option as of version 1.6... – Jacob Oct 02 '14 at 12:53
  • 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 Oct 06 '14 at 07:41
  • This procedure doesn't work when begin of file is damaged. – Znik Nov 27 '15 at 14:40
  • This is particularly painful if you need to gunzip a bunch of files in a dir. Easy enough to use loop but then it gets messy dropping the .gz off the end of the filename – MikeKulls Mar 10 '22 at 23:23
68

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.

Kevin B
  • 13
  • 5
Stef
  • 781
  • 4
  • 3
  • 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. –  Sep 13 '10 at 22:01
43

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

gunzip < myfile.gz > myfile
retracile
  • 3,416
  • 3
  • 24
  • 23
  • 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 Dec 09 '16 at 01:11
  • It may be worth noting that you can also pipe the output, like: `gunzip < database.sql.gz | mysql -uroot -p` – Timothy Zorn Jun 07 '20 at 23:10
18
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.
  • 420
  • 1
  • 6
  • 11
13

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
  • 2,084
  • 1
  • 14
  • 9
5

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

gunzip -c myfile.gz > myfile
mob
  • 262
  • 1
  • 8
2

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

Justin Smith
  • 4,116
  • 1
  • 21
  • 16