How to gzip a file in linux

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: In Linux, you can gzip a file using the `gzip` command followed by the filename. For example, `gzip mydocument.txt` will compress `mydocument.txt` into `mydocument.txt.gz`. To compress a file and keep the original, use the `gzip -k` option.

Key Facts

Overview

Compressing files is a common practice in computing for several reasons, including saving storage space and reducing the time it takes to transfer files over a network. In the Linux operating system, the gzip utility is a widely used and efficient tool for this purpose. It employs the DEFLATE algorithm, which is a combination of the LZ77 algorithm and Huffman coding, to achieve significant file size reduction.

What is Gzip?

gzip stands for GNU zip. It is a free software utility that compresses and decompresses files. It's a command-line tool, meaning you interact with it by typing commands into the terminal. gzip is a staple in Unix-like operating systems, including Linux and macOS, and is often used in conjunction with the tar utility for archiving multiple files before compression (creating .tar.gz or .tgz files).

How to Gzip a File

The most basic way to compress a file using gzip is to simply type the command followed by the name of the file you want to compress.

Basic Compression

To compress a file named mydocument.txt, you would open your terminal and type:

gzip mydocument.txt

After running this command, the original file mydocument.txt will be removed, and a new file named mydocument.txt.gz will be created. This new file is the compressed version.

Keeping the Original File

Sometimes, you might want to keep the original file after compression. You can achieve this using the -k or --keep option:

gzip -k mydocument.txt

This command will create mydocument.txt.gz but leave mydocument.txt intact.

Compressing Multiple Files

You can compress multiple files at once by listing them after the gzip command:

gzip file1.txt file2.log file3.csv

Alternatively, you can use wildcards to compress files that match a pattern. For instance, to compress all files ending with .txt in the current directory:

gzip *.txt

In both cases, each specified file will be compressed individually, and the original files will be deleted by default.

Compressing Recursively (Directories)

If you need to compress all files within a directory and its subdirectories, you can use the -r or --recursive option:

gzip -r my_directory

This command will go through my_directory and all its subdirectories, compressing every file it finds. Note that this will create compressed versions of files inside the directory structure, not compress the directory itself into a single archive file.

Controlling Compression Level

gzip allows you to specify the compression level, ranging from 1 (fastest compression, least compression) to 9 (slowest compression, best compression). The default level is 6.

Higher compression levels take more CPU time and memory but result in smaller files.

How to Decompress Gzip Files

To decompress a file compressed with gzip (a file ending in .gz), you use the gunzip command or the gzip -d option.

Using gunzip

To decompress mydocument.txt.gz:

gunzip mydocument.txt.gz

This will remove the .gz file and restore the original mydocument.txt.

Using gzip -d

The -d or --decompress option with gzip performs the same function:

gzip -d mydocument.txt.gz

Like gunzip, this command also deletes the .gz file and recreates the original.

Keeping the Compressed File After Decompression

Similar to compression, you can keep the compressed file after decompressing using the -k option with gzip -d:

gzip -dk mydocument.txt.gz

Common Use Cases

Difference Between gzip and tar

It's important to distinguish gzip from tar (Tape Archiver). tar is used to bundle multiple files and directories into a single archive file (often called a tarball), but it does not compress them. gzip, on the other hand, compresses a single file. They are often used together: you first use tar to create a single archive, and then you use gzip to compress that archive. This results in a .tar.gz or .tgz file, which is a common format for distributing software and data on Linux.

Example of creating and compressing a tarball:

# Create a tarball named archive.tar from all .txt filestar -cvf archive.tar *.txt# Compress archive.tar using gzipgzip archive.tar# This creates archive.tar.gz

Alternatively, you can use the -z option with tar to perform both archiving and gzip compression in one step:

tar -czvf archive.tar.gz *.txt

Conclusion

The gzip command is a fundamental tool for file compression in Linux, offering a simple yet effective way to reduce file sizes. Understanding its basic usage, options like keeping original files, recursive compression, and its relationship with tar will significantly enhance your command-line efficiency.

Sources

  1. gzip(1) - Linux man pageCC0-1.0
  2. Gzip - GNU Project - Free Software FoundationGPL-3.0-or-later
  3. Gzip - WikipediaCC-BY-SA-4.0

Missing an answer?

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