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 cd Command: Change the Current Directory

Linux cd Command: Change the Current Directory

Learn how to use the Linux cd command to move between directories using absolute, relative, home, and previous paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.

Linux Tutorial / Compare Files with diff and cmp

Linux Tutorial / Compare Files with diff and cmp

Use diff and cmp to compare two files, read unified diff output, and distinguish a detected difference from an actual command error.

Linux screen Command: Create and Manage Detachable Terminal Sessions

Linux screen Command: Create and Manage Detachable Terminal Sessions

Learn how to use the Linux screen command to keep terminal sessions running and reconnect to them later, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Best Linux Distributions for Web Servers: Ubuntu, Debian, or Rocky Linux?

Compare Ubuntu Server, Debian, and Rocky Linux for web hosting based on support lifecycle, package freshness, documentation, operational stability, and application requirements.

Linux mv Command: Move and Rename Files and Directories

Linux mv Command: Move and Rename Files and Directories

Learn how to move and rename files or directories with Linux mv, avoid unwanted overwrites, clarify destination paths, and handle cross-filesystem moves.

Linux realpath Command: Resolve Absolute and Canonical Paths

Linux realpath Command: Resolve Absolute and Canonical Paths

Learn how to use the Linux realpath command to resolve relative paths, symbolic links, and redundant path components, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux zypper Command: Manage Packages on openSUSE and SUSE

Linux zypper Command: Manage Packages on openSUSE and SUSE

Learn how to manage Packages on openSUSE and SUSE with the Linux zypper command, including practical examples, key options, and important precautions.

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Linux Tutorial / Standard Input, Output, Redirection, and Pipes

Understand standard input, output, and error; practice file redirection and pipes; and learn why overwrite risk and redirection order matter.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.