How to gzip and keep original file on Unix or Linux command line

I would like to compress a log file using gzip Unix command line utility, and I would also like to keep the original file. However, when I use the gzip my-app.log command, results in modifying my log file and renaming it my-app.log.gz. How do I force the gzip command to keep original file while maintaining the original file on Linux or Unix-like system?

The gzip program compresses and decompresses files on Unix like system. You need to pass the -c or --stdout, or --to-stdout option to the gzip command. This option specifies that output will go to the standard output stream, leaving original files intact.

Syntax: To keep original file while using gzip

The options are as follows:
gzip -c input.file > output.file.gz
If no files are specified and in direction used, gzip will compress from standard input, or decompress to standard output. So one can use the following syntax:
gzip < input.file > output.file.gz
Or pass the -k/--keep to the gzip command to leep (don’t delete) input files during compression or decompression:
gzip -k input.file
gzip --keep input.file

Examples that shows how to gzip and keep original file on Linux or Unix

Let us tell gzip command to keep original file called Friday-Comic.jpg:
$ ls -lh Friday-Comic.jpg
Gzip and create a new file called Friday-Comic-1.jpg.gz:
$ gzip -c Friday-Comic.jpg > Friday-Comic-1.jpg.gz
$ ls -lh Friday-Comic-1.jpg.gz

Gzip and create a new file called Friday-Comic-1.jpg.gz using shell redirection:
$ gzip < Friday-Comic.jpg > Friday-Comic-2.jpg.gz
$ ls -lh Friday-Comic*

Sample outputs:

Fig.01: Tell gzip command to keep original file on Unix or Linux system

Fig.01: Tell gzip command to keep original file on Unix or Linux system

As I said earlier the simplest option is to either pass the -k or --kbd option:
gzip --keep my-filename.doc

Conclusion

We learned that Linux and Unix’s gzip command reduces the named files’ size using the Lempel-Ziv coding (LZ77) algorithm. Whenever possible, each file is replaced by one with the extension .gz while keeping the same ownership modes, access, and modification times. To avoid that, we can pass the option or use the shell redirection. For more erudition, see the gzip command command man page by typing the following man command:
$ man gzip


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Pickfire Mar 22, 2016 @ 13:53

    You can used gzip -k input too.

    • 🐧 Vivek Gite Mar 25, 2016 @ 10:24

      Thanks for the tip. The faq has been updated. I appreciate the comment.

  • Student May 9, 2016 @ 21:05

    Thanks that’s exactly what i was searching for, i find this website to be increasingly useful

  • Anonymous Oct 21, 2020 @ 6:22

    what will be filename of unzipped file ?It should be the filename which you zipped right ?
    If you try to unzip any zip file it will not be renamed to original filename – in this case it will be Friday-Comic-1.jpg and not Friday-Comic.jpg.
    any idea why ?

    [oracle@16958adba7a3 tmp]$ touch Friday-Comic.jpg
    [oracle@16958adba7a3 tmp]$ ls -lh Friday-Comic.jpg

    -rw-r--r-- 1 oracle oinstall 0 Oct 21 06:20 Friday-Comic.jpg

    And
    [oracle@16958adba7a3 tmp]$ gzip -c Friday-Comic.jpg > Friday-Comic-1.jpg.gz
    [oracle@16958adba7a3 tmp]$ ls -lhrt

    total 8.0K
    drwxr-x--- 3 oracle oinstall 4.0K Oct 14 07:40 CVU_19.0.0.0.0_oracle
    drwxr-x--- 2 oracle oinstall    6 Oct 19 14:26 hsperfdata_oracle
    -rw-r--r-- 1 oracle oinstall    0 Oct 21 06:20 Friday-Comic.jpg
    -rw-r--r-- 1 oracle oinstall   37 Oct 21 06:20 Friday-Comic-1.jpg.gz

    And:
    [oracle@16958adba7a3 tmp]$ rm Friday-Comic.jpg
    [oracle@16958adba7a3 tmp]$ gunzip Friday-Comic-1.jpg.gz
    [oracle@16958adba7a3 tmp]$ ls -lhrt

    total 4.0K
    drwxr-x--- 3 oracle oinstall 4.0K Oct 14 07:40 CVU_19.0.0.0.0_oracle
    drwxr-x--- 2 oracle oinstall    6 Oct 19 14:26 hsperfdata_oracle
    -rw-r--r-- 1 oracle oinstall    0 Oct 21 06:20 Friday-Comic-1.jpg
    • 🐧 Vivek Gite Oct 21, 2020 @ 11:01

      Use the -k option if supported:
      gzip -k filename

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum