How to tell gzip to keep original file?

292,147

Solution 1

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.txt > file.txt.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.txt > file.txt.gz is that if file.txt can't be opened, the command will fail without creating an empty file.txt.gz (or overwriting an existing file.txt.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.txt's metadata (ownership, permissions, modification time, name of uncompressed file) to file.txt.gz.

Also if file.txt.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).

Solution 2

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.

Solution 3

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"
    fi
}

and then

gzipkeep file.txt
Share:
292,147

Related videos on Youtube

Manuel Selva
Author by

Manuel Selva

Computer scientist

Updated on September 18, 2022

Comments

  • Manuel Selva
    Manuel Selva over 1 year

    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 ?

    • SHW
      SHW almost 12 years
      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
    • Manuel Selva
      Manuel Selva almost 12 years
      @SHW I didn't get your comment ... ?
    • daisy
      daisy almost 12 years
      @ManuelSelva once the original file is compressed, it's no longer needed, I guess that was the design.
    • Mateen Ulhaq
      Mateen Ulhaq over 5 years
      Why gzip/gunzip do this by default when no other unix tool does this is beyond me.
  • codewaggle
    codewaggle over 11 years
    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.
  • user3174706
    user3174706 almost 11 years
    Geez. RHEL6 only comes with zip 1.3.12 in base...
  • Stéphane Chazelas
    Stéphane Chazelas over 10 years
    Note that if the file is -, you have to call it as gzipkeep ./-.
  • paul
    paul almost 8 years
    This works on Mac/OS X :) (which is BSD)
  • MichaelChirico
    MichaelChirico over 6 years
    Are there many standard distros not using >= 1.6 anymore? Wondering if answer needs updat
  • flow2k
    flow2k almost 6 years
    What's the -k option? Couldn't find it in the man pages, e.g. this version here linux.die.net/man/1/gzip. Also, it looks like gzip copies the file's metadata by default.
  • flow2k
    flow2k almost 6 years
    ah, I see it on my macos but too bad it's not on RHEL nor Ubuntu. Thanks.
  • notilas
    notilas over 5 years
    What is "--" for? I think gzip -k "file" works fine.
  • don_crissti
    don_crissti over 5 years
  • killjoy
    killjoy over 5 years
    This was ideal for me, and allowed me to one-line my program logic. gunzip < $log | grep foo | gzip > foo-$log.gz
  • Sathya
    Sathya about 5 years
    @MichaelChirico CentOS 7. :(
  • GypsyCosmonaut
    GypsyCosmonaut almost 5 years
    @StéphaneChazelas Working on gzip version 1.4 as well. cat file | gzip > file.gz
  • Fonic
    Fonic over 3 years
    This is the best solution for scripts IMO, as you can be sure the output file > "$1.gz" will exist exactly like specified. Furthermore, you can also specify a different name if the need should arise.
  • Isin Altinkaya
    Isin Altinkaya about 2 years
    It can be put to .bashrc as a small function gzik(){gzip < ${1} > ${1}.gz}
  • Admin
    Admin almost 2 years
    @IsinAltinkaya, gzik(){gzip < ${1} > ${1}.gz} is zsh syntax. In bash, you need gzik() { gzip < "$1" > "$1".gz; }