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
Key Facts
- The `gzip` command is the standard utility for compressing files in Linux and Unix-like systems.
- Compression reduces file size, saving disk space and speeding up file transfers.
- The original file is replaced by default when using `gzip`.
- To keep the original file, use the `-k` or `--keep` option with `gzip`.
- The `gunzip` command is used to decompress files created by `gzip`.
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.txtAfter 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.txtOr, using the long form of the option:
gzip --keep document.txtExecuting 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.dataThis 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/directoryHere, `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.txtTo use the fastest compression (level 1):
gzip -1 document.txtUsing 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.gzThis 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.gzThis will replace `document.txt.gz` with the original `document.txt`.
Using `gzip -d`:
gzip -d document.txt.gzThis 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.gzWhen to Use Gzip
gzip is ideal for compressing individual files. It's commonly used for:
- Reducing the size of log files.
- Compressing configuration files before backing them up.
- Making individual large files more manageable for transfer.
- As a component in build processes (e.g., compressing documentation).
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.
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 pageCC0-1.0
- Gzip - GNU Project - Free Software FoundationGPL-3.0-or-later
Missing an answer?
Suggest a question and we'll generate an answer for it.