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
Key Facts
- Gzip is a file format and a software application used for compression and decompression.
- It was developed by Mark Adler and Jean-Louis Gailly and released in 1992.
- Gzip is a lossless data compression algorithm.
- It is widely used on Unix-like operating systems.
- The typical file extension for gzipped files is `.gz`.
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.txtThis 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.txtTo compress multiple files at once, you can list them, but `gzip` will compress each individually:
gzip file1.txt file2.log file3.csvThis 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_compressThen, compress the tar archive with gzip:
gzip archive_name.tarThis will result in `archive_name.tar.gz`. Many systems offer a shortcut to combine these steps:
tar -czvf archive_name.tar.gz directory_to_compressThe `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.gzSimilar 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.gzAlternatively, you can use the `gzip` command with the `-d` (decompress) flag:
gzip -d filename.txt.gzThis 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.gzThis 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:
- Bzip2: Offers better compression ratios than Gzip but is generally slower. Files typically have a `.bz2` extension.
- XZ: Provides even higher compression ratios, often exceeding Bzip2, but is typically the slowest. Files usually have a `.xz` extension.
- Zstandard (Zstd): Developed by Facebook, Zstd aims to offer compression ratios similar to Gzip but with significantly faster compression and decompression speeds.
- 7-Zip: A popular archiving tool that uses its own `.7z` format, offering very high compression ratios.
Gzip remains a widely used standard due to its balance of speed, compression ratio, and ubiquitous support across systems.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Gzip - WikipediaCC-BY-SA-4.0
- gzip(1) - Linux man pagefair-use
Missing an answer?
Suggest a question and we'll generate an answer for it.