How to gzip

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To gzip a file, you typically use the `gzip` command in your terminal. For example, `gzip filename.txt` will compress the file and create `filename.txt.gz`. To decompress, use `gunzip filename.txt.gz` or `gzip -d filename.txt.gz`.

Key Facts

What is Gzip?

Gzip, short for GNU zip, is a popular file compression and decompression utility. It's a software application freely available under the GNU General Public License. Gzip works by using the DEFLATE compression algorithm, which is a combination of the LZ77 algorithm and Huffman coding. This algorithm is lossless, meaning that no data is lost during the compression and decompression process; the original file can be perfectly reconstructed from the compressed version.

Developed by Mark Adler and Jean-Louis Gailly, Gzip was first released in 1992. It quickly became a standard tool for managing file sizes, especially on Unix-like operating systems, due to its efficiency and open-source nature. It's commonly used for reducing the size of text files, log files, and program source code, making them easier to store, transfer, and download.

How to Use Gzip from the Command Line

The most common way to use Gzip is through the command-line interface (CLI) on Unix-like systems (Linux, macOS) or via tools like Cygwin or Git Bash on Windows.

Compressing Files

To compress a single file, you use the `gzip` command followed by the filename:

gzip filename.txt

This command will compress `filename.txt` and replace it with `filename.txt.gz`. The original file is deleted by default after successful compression. If you want to keep the original file, you can use the `-k` or `--keep` option:

gzip -k filename.txt

To compress multiple files at once, you can list them, but `gzip` will compress each individually:

gzip file1.txt file2.log file3.csv

This will create `file1.txt.gz`, `file2.log.gz`, and `file3.csv.gz`.

Compressing Directories

Gzip itself does not compress directories directly. To compress a directory, you first need to archive it into a single file, typically using the `tar` command, and then compress the resulting tarball.

First, create a tar archive:

tar -cvf archive_name.tar directory_to_compress

Then, compress the tar archive with gzip:

gzip archive_name.tar

This will result in `archive_name.tar.gz`. Many systems offer a shortcut to combine these steps:

tar -czvf archive_name.tar.gz directory_to_compress

The `z` flag tells `tar` to use gzip compression.

Decompressing Files

To decompress a file ending in `.gz`, you can use the `gunzip` command:

gunzip filename.txt.gz

Similar to `gzip`, this command will replace `filename.txt.gz` with the original `filename.txt` and delete the compressed file. If you wish to keep the compressed file, use the `-k` option:

gunzip -k filename.txt.gz

Alternatively, you can use the `gzip` command with the `-d` (decompress) flag:

gzip -d filename.txt.gz

This achieves the same result as `gunzip`.

Decompressing Tar Gzip Archives

To decompress a `.tar.gz` or `.tgz` file, you use the `tar` command with the `z` flag for gzip decompression:

tar -xzvf archive_name.tar.gz

This command will extract the contents of the archive into the current directory.

Gzip in Web Servers

Gzip compression is extensively used in web development to reduce the size of text-based resources like HTML, CSS, and JavaScript files before sending them from the web server to the browser. This significantly speeds up page load times. Most modern web servers (like Apache, Nginx, and IIS) can be configured to automatically gzip files before transmission. Browsers send an `Accept-Encoding: gzip` header to indicate they support and prefer gzip-compressed content.

Other Compression Tools

While Gzip is very common, other compression utilities exist, such as:

Gzip remains a widely used standard due to its balance of speed, compression ratio, and ubiquitous support across systems.

Sources

  1. Gzip - WikipediaCC-BY-SA-4.0
  2. gzip(1) - Linux man pagefair-use

Missing an answer?

Suggest a question and we'll generate an answer for it.