How to gzip a file
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
- The `gzip` command is a standard utility on most Linux and macOS systems.
- On Windows, you can use built-in tools like File Explorer's compression feature or download third-party software like 7-Zip or WinRAR.
- Gzipping typically reduces file size by 40-70%, depending on the file's content.
- The original file is usually deleted by default after successful compression with the `gzip` command, unless the `-k` (keep) option is used.
- To decompress a gzipped file, you can use the `gunzip` command or the `gzip -d` command.
Overview
Gzipping a file is a common method for reducing its storage space and making it faster to transfer over networks. It's a form of lossless data compression, meaning no information is lost during the compression and decompression process. The most common tool used for this is the `gzip` utility, which is widely available on Unix-like operating systems.
How to Gzip a File on Linux/macOS
On Linux and macOS, the `gzip` command is the primary tool. To compress a file named my_document.txt, you would open your terminal and type:
gzip my_document.txt
This command will:
- Compress
my_document.txt. - Create a new file named
my_document.txt.gz. - By default, delete the original
my_document.txtfile.
If you wish to keep the original file after compression, you can use the -k or --keep option:
gzip -k my_document.txt
This will result in both my_document.txt and my_document.txt.gz existing.
To compress multiple files at once, you can use a wildcard:
gzip *.txt
This will compress all files ending with the .txt extension in the current directory.
How to Gzip a File on Windows
Windows does not have a built-in command-line utility named `gzip` by default, but there are several ways to achieve the same result:
- Using File Explorer (for .zip, not .gz): While not strictly `gzip`, Windows File Explorer allows you to compress files into
.ziparchives. Right-click on the file(s), select "Send to," and then "Compressed (zipped) folder." This is a common alternative for general compression. - Using Third-Party Software: Many free and paid applications support the GZIP format and other compression formats. Popular choices include:
- 7-Zip: A free and open-source file archiver that supports GZIP (.gz) compression. You can download it from
www.7-zip.org. After installation, you can right-click on a file, go to "7-Zip," and select "Add to archive..." choosing the desired format. - WinRAR: A popular commercial file archiver that also supports GZIP.
- PeaZip: Another free file archiver with broad format support.
- 7-Zip: A free and open-source file archiver that supports GZIP (.gz) compression. You can download it from
- Using Windows Subsystem for Linux (WSL): If you have WSL installed, you can access the Linux `gzip` command directly from your Windows environment.
Understanding GZIP Compression
GZIP is based on the DEFLATE compression algorithm, which is a combination of the LZ77 algorithm and Huffman coding. It's highly efficient for compressing text files, logs, and other data that contains repetitive patterns. While effective, the compression ratio can vary significantly:
- Text files, logs, code: Often achieve high compression ratios (e.g., 60-80%).
- Already compressed files (like JPEGs, MP3s, ZIPs): Will see little to no benefit from GZIP and may even slightly increase in size due to overhead.
- Binary files: Compression varies depending on the file's internal structure.
The typical compression ratio for general-purpose files is often cited as being around 40-70% reduction in size.
Decompressing Gzipped Files
To decompress a file that has been gzipped, you can use the `gunzip` command or `gzip -d` on Linux/macOS:
gunzip my_document.txt.gz
or
gzip -d my_document.txt.gz
Both commands will replace my_document.txt.gz with the original my_document.txt file.
On Windows, third-party tools like 7-Zip or WinRAR can be used to extract `.gz` files. Simply right-click the `.gz` file, select the compression tool, and choose an "Extract" option.
When to Use GZIP
GZIP is particularly useful for:
- Archiving: Creating smaller archives for long-term storage.
- Web Servers: Many web servers are configured to compress content (like HTML, CSS, JavaScript) on the fly using GZIP before sending it to the browser, significantly speeding up page load times.
- Log Files: Compressing large log files to save disk space.
- Data Transfer: Reducing the size of files before uploading or downloading them.
It's important to note that GZIP typically compresses a single file. If you need to compress multiple files into a single archive, you would usually combine them first using a tool like `tar` (on Unix-like systems) and then gzip the resulting `.tar` file, creating a `.tar.gz` or `.tgz` archive.
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(1) - Linux man pageCC-BY-SA-4.0
- Gzip - GNU Projectfair-use
- Gzip - WikipediaCC-BY-SA-4.0
Missing an answer?
Suggest a question and we'll generate an answer for it.