Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I would like to compress a text file using gzip command line tool while keeping the original file. By default running the following command

gzip file.txt

results in modifying this file and renaming it file.txt.gz. instead of this behavior I would like to have this new compressed file in addition to the existing one file.txt. For now I am using the following command to do that

gzip -c file.txt > file.txt.gz

It works but I am wondering why there is no easier solution to do such a common task ? Maybe I missed the option doing that ?

share|improve this question
This is because, gzip compresses the given file and creates new file. Compression means squeezing original file and replacing it with new one. Your "-c" option is explicitly telling gzip to save it with other name. That's why it works –  SHW Aug 31 '12 at 8:52
@SHW I didn't get your comment ... ? –  Manuel Selva Aug 31 '12 at 9:07
@ManuelSelva once the original file is compressed, it's no longer needed, I guess that was the design. –  warl0ck Aug 31 '12 at 9:58

4 Answers 4

up vote 106 down vote accepted

For GNU gzip 1.6 or above, FreeBSD and derivatives or recent versions of NetBSD, see don_cristi's answer.

With any version, you can use shell redirections as in:

gzip < file > file.gz

When not given any argument, gzip reads its standard input, compresses it and writes the compressed version to its standard output. As a bonus, when using shell redirections, you don't have to worry about files called "--help" or "-" (that latter one still being a problem for gzip -c --).

Another benefit over gzip -c file > file.gz is that if file can't be opened, the command will fail without creating an empty file.gz (or truncating an already existing file.gz) and without running gzip at all.

A significant difference compared to gzip -k though is that there will be no attempt at copying the file's metadata (ownership, permissions, modification time) to file.gz.

Also if file.gz already existed, it will silently override it unless you have turned the noclobber option on in your shell (with set -o noclobber for instance in POSIX shells).

share|improve this answer
Now this addresses the specifics of the OP's question and answers the general question "How to tell gzip to keep the original file". Very handy and more useful than -c. –  codewaggle Dec 18 '12 at 21:20
This should be solution over the accepted answer, as it is less complex and more quickly answers base question. –  Dan Ciborowski - MSFT Jul 5 '13 at 1:19
Note that the < and > are actually supposed to be typed into the command and are not representing a placeholder for the filename –  Gaurav Gupta Feb 23 at 11:32

Note that the recently (June 2013) released gzip-1.6 "accepts the --keep (-k) option, for consistency with tools like xz, lzip and bzip2. With this option, gzip no longer removes named input files when compressing or decompressing".

Excerpt from the man page:

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

So, as of 1.6, you can use -k or --keep to keep the original file:

gzip -k -- "$file"

(note that it doesn't work if $file is - (which gzip interprets as meaning stdin instead of the actual file called -), in which case, you have to change it to ./-)

That option was first introduced in the FreeBSD implementation of gzip (in FreeBSD 7.0 in 2007) for consistency with bzip2. That gzip is based on a rewrite of GNU gzip by NetBSD. The -k option eventually made it back to NetBSD in 2010.

share|improve this answer
Geez. RHEL6 only comes with zip 1.3.12 in base... –  Nicholas Tolley Cottrell Jun 25 '13 at 14:36

From the documentation it seems that there is no option to create a copy of the file.

You can define a shell function

gzipkeep() {
    if [ -f "$1" ] ; then
        gzip -c -- "$1" > "$1.gz"

and then

gzipkeep file.txt
share|improve this answer
Note that if the file is -, you have to call it as gzipkeep ./-. –  Stéphane Chazelas Jan 7 '14 at 17:13

So, to (finally) answer your actual question ... for 1.6 (or later) see the excellent answer noted (, else ... you're already doing as best as can be done - there is no "don't delete my file" option, except, as you noted, via -c.

In my case I have to do gzip -dc {file} | tar xvz.

Don't ask me ... (alias unpack='tar xvz') works most places, but on some freefilesync .tar.gz downloads, that doesn't work.

BTW Thank you for having posed the question here. Ran into this myself, google quickly hit your question ... and thus quickly hit the answer.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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