284

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

3
  • 6
    use the following command: gunzip -k
    – fazineroso
    Aug 3, 2015 at 13:45
  • what gunzip has -k option? I see no one.
    – Znik
    Nov 27, 2015 at 14:42
  • 1
    @Znik, gzip/gunzip version 1.6 and higher has the option -k. Sep 14, 2016 at 11:48

7 Answers 7

295

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
12
  • 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, 2009 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, 2009 at 16:15
  • 32
    it's probably just zcat on your system.
    – user23307
    Jan 31, 2010 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 8, 2011 at 20:19
  • 1
    You can also add the line alias gzcat="gunzip -c" to your .bashrc Mar 30, 2012 at 20:04
69

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.

1
  • 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, 2010 at 22:01
45

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

gunzip < myfile.gz > myfile
2
  • 1
    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 9, 2016 at 1:11
  • It may be worth noting that you can also pipe the output, like: gunzip < database.sql.gz | mysql -uroot -p Jun 7, 2020 at 23:10
21
gzip -dk myfile.gz

OR

gunzip -k myfile.gz

Comments:

   -k --keep    Keep (don't delete) input files during compression or decompression.
4
  • 1
    which version of gunzip are you using? gzip 1.5 from GNU has not the -k option
    – Rho Phi
    Feb 3, 2020 at 12:01
  • gzip: invalid option -- 'k'
    – Putnik
    Feb 6, 2020 at 9:44
  • @RhoPhi gzip and gunzip version: 1.10 , I use git bash (MINGW64) in windows Feb 10, 2020 at 10:18
  • @Putnik I use 1.10 Feb 10, 2020 at 10:18
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.

3
  • 8
    or just do: tar xzvf myfile.tar.gz Sep 23, 2009 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, 2009 at 20:01
  • @JoakimElofsson deserves a medal !
    – Jono
    Oct 10, 2020 at 16:17
5

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

gunzip -c myfile.gz > myfile
2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .