How to gz 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: To "gz" a file in Linux, you typically use the `gzip` command. This command compresses a file, replacing the original with a compressed version that has a `.gz` extension. For example, to compress `myfile.txt`, you would run `gzip myfile.txt`.

Key Facts

Overview

In Linux and other Unix-like operating systems, compressing files is a common practice for several reasons. It helps to save disk space, making it more efficient to store large amounts of data. It also speeds up the process of transferring files over networks, as smaller files require less bandwidth and time to transmit. One of the most widely used and fundamental compression utilities is `gzip`.

The term "gz a file" is shorthand for compressing a file using the `gzip` utility. This process creates a new file that contains the original data in a more compact form. The standard convention is that a compressed file will have the `.gz` extension appended to its original filename.

How to GZ a File in Linux

Using the `gzip` Command

The primary command for compressing files with gzip is, unsurprisingly, `gzip`. Its basic syntax is straightforward:

gzip [OPTION]... [FILE]...

When you execute `gzip` on a file, it performs the compression and, by default, replaces the original file with the compressed version. For instance, if you have a file named `document.txt` and you want to compress it, you would open your terminal and type:

gzip document.txt

After this command runs successfully, `document.txt` will no longer exist in that directory. Instead, you will find a new file named `document.txt.gz`, which is the compressed representation of your original document.

Keeping the Original File

In many scenarios, you might want to compress a file but retain the original uncompressed version for immediate use or as a backup. The `gzip` command provides an option for this purpose: the `-k` or `--keep` flag.

To compress `document.txt` while ensuring the original file remains intact, you would use:

gzip -k document.txt

Or, using the long form of the option:

gzip --keep document.txt

Executing this command will create `document.txt.gz` and leave `document.txt` untouched in the directory.

Compressing Multiple Files

You can compress multiple files at once by listing them after the `gzip` command. However, be aware that `gzip` is designed to compress a single file at a time. If you provide multiple filenames, it will compress each one individually, creating separate `.gz` files for each.

gzip file1.txt file2.log file3.data

This command would result in `file1.txt.gz`, `file2.log.gz`, and `file3.data.gz`.

If you need to compress an entire directory and its contents into a single archive file, `gzip` alone is not the tool for that. For archiving directories, you would typically use `tar` in conjunction with `gzip`. The common pattern is to create a `.tar` archive first and then compress that archive using `gzip`, resulting in a `.tar.gz` or `.tgz` file.

tar -czvf archive_name.tar.gz /path/to/directory

Here, `tar` creates (`c`) the archive, compresses it with gzip (`z`), specifies the output filename (`f`), and shows verbose output (`v`).

Controlling Compression Level

The `gzip` command offers different compression levels, allowing you to trade off compression speed for file size. The default level is usually 6. You can specify a level from 1 (fastest compression, largest file) to 9 (slowest compression, smallest file).

To use the highest compression level (level 9):

gzip -9 document.txt

To use the fastest compression (level 1):

gzip -1 document.txt

Using higher compression levels takes more CPU time and can be noticeably slower, especially for large files.

Viewing Compressed Files Without Decompressing

Sometimes, you might want to quickly inspect the content of a `.gz` file without fully decompressing it. The `zcat` command (or `gzcat`) is useful for this. It works like `cat` but handles `.gz` files directly.

zcat document.txt.gz

This command will print the uncompressed content of `document.txt.gz` to your standard output.

Decompressing GZipped Files

To reverse the process and decompress a `.gz` file, you use the `gunzip` command or the `gzip -d` option.

Using `gunzip`:

gunzip document.txt.gz

This will replace `document.txt.gz` with the original `document.txt`.

Using `gzip -d`:

gzip -d document.txt.gz

This achieves the same result as `gunzip`.

Similar to `gzip`, `gunzip` also has a `-k` option to keep the compressed file after decompression:

gunzip -k document.txt.gz

When to Use Gzip

gzip is ideal for compressing individual files. It's commonly used for:

For archiving multiple files or directories into a single compressed file, tools like `tar` combined with `gzip` (creating `.tar.gz` files) or `zip` are more appropriate.

Sources

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

Missing an answer?

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