Linux gzip Command: Compress a File in gzip Format

gzip compresses file data into the .gz format. It does not combine multiple files into one archive and usually converts the original file into a compressed file.

What is the gzip command?

When you specify the file to compress, usually a file.gz is created and the original file is removed. To keep the original, use -k or send it to standard output with -c and save it in a separate file. To compress while preserving directory structure, it is appropriate to first bundle with tar.

Basic syntax

gzip [options] [file...]

Installed implementations and options may vary depending on the distribution. Check the current system's description with man gzip.

Examples

Compress while keeping the original

Use -k to keep the original and create a new .gz file.

gzip -k report.log

Save the compression result under a different name

-c sends the compressed data to standard output without changing the original. Make sure the output file does not already exist.

gzip -c report.log > report-copy.log.gz

Compressed File Integrity Check

Checks for gzip stream errors without extracting the file contents.

gzip -t report.log.gz

Check if the original file remains after compression

The gzip file command by default replaces the original with a .gz file. If you want to keep the original as well, use GNU gzip's -k option.

printf 'hello\n' > note.txt
gzip -k note.txt
ls note.txt*

Example output:

note.txt  note.txt.gz

Compression success itself usually does not display anything on the screen. You can check the structure of a compressed file with gzip -t note.txt.gz, but it does not guarantee that it is identical to the original content. If you want to combine multiple files into one, use tar first and then apply gzip.

Main Options and Format

Options/Format Description
-k Keeps the input file after compression.
-c Sends the result to standard output.
-t Verifies the integrity of compressed data.
-1 ... -9 Chooses a level from speed priority to compression ratio priority.
-v Displays information about processing, such as compression ratio.

Precautions when using

The default execution replaces the original file, so use -k when you need to keep the original. Even if multiple files are given as arguments, they will not be combined into a single .gz. gzip compression is not encryption and should not be used for protecting confidential materials.

Frequently Asked Questions

Can you use gzip to make an entire directory into a single file?

gzip itself is not an archiver that contains directory structures. Use it together with tar like tar -czf backup.tar.gz directory/.

Official Documentation

You can check the exact behavior of options and implementation differences in the official documentation for gzip.

More in This Category
Linux sort Command: Sort Text Lines

Linux sort Command: Sort Text Lines

Learn how to sort text lines with the Linux sort command, including practical examples, key options, and important precautions.

Linux nmap Command: Scan Authorized Hosts and Ports

Linux nmap Command: Scan Authorized Hosts and Ports

Learn how to scan Authorized Hosts and Ports with the Linux nmap command, including practical examples, key options, and important precautions.

Linux getent Command: Query Users, Groups, and Other NSS Databases

Linux getent Command: Query Users, Groups, and Other NSS Databases

Learn how to query Users, Groups, and Other NSS Databases with the Linux getent command, including practical examples, key options, and important precautions.

Linux fmt Command: Reformat Plain-Text Paragraphs

Linux fmt Command: Reformat Plain-Text Paragraphs

Learn how to reformat Plain-Text Paragraphs with the Linux fmt command, including practical examples, key options, and important precautions.

Linux comm Command: Compare Two Sorted Files

Linux comm Command: Compare Two Sorted Files

Learn how to compare Two Sorted Files with the Linux comm command, including practical examples, key options, and important precautions.

Linux hostnamectl Command: Show and Change the Host Name

Linux hostnamectl Command: Show and Change the Host Name

Learn how to show and Change the Host Name with the Linux hostnamectl command, including practical examples, key options, and important precautions.

Linux ethtool Command: Inspect Network Link and Driver Settings

Linux ethtool Command: Inspect Network Link and Driver Settings

Learn how to inspect Network Link and Driver Settings with the Linux ethtool command, including practical examples, key options, and important precautions.

Linux Tutorial / Understand Users, Groups, and File Ownership

Linux Tutorial / Understand Users, Groups, and File Ownership

Understand UIDs, GIDs, primary and supplementary groups, and file ownership, then inspect them safely with id, groups, getent, ls, and stat.

Linux timedatectl Command: Check Time Zone and Time Synchronization

Linux timedatectl Command: Check Time Zone and Time Synchronization

Learn how to check Time Zone and Time Synchronization with the Linux timedatectl command, including practical examples, key options, and important precautions.

Linux printf Command: Print Values with a Defined Format

Linux printf Command: Print Values with a Defined Format

Learn how to print Values with a Defined Format with the Linux printf command, including practical examples, key options, and important precautions.