How To Gzip A File In Linux : Single File Compression Command

Running `gzip filename.txt` from your terminal compresses the file into a `.gz` archive. If you are wondering how to gzip a file in linux, this command is the quickest way to reduce file size and save disk space. Gzip is a standard compression tool on most Linux distributions, and it works seamlessly with other commands like `tar` for archiving multiple files.

This guide walks you through everything you need to know about gzip, from basic single-file compression to advanced options like preserving original files and setting compression levels. You will learn practical commands, common pitfalls, and how to integrate gzip into your daily workflow.

How To Gzip A File In Linux

Gzip stands for GNU zip, and it is one of the most popular compression utilities on Linux. It uses the DEFLATE algorithm to shrink file sizes without losing any data. The compressed file gets a `.gz` extension, and the original file is replaced by default.

Let us start with the most basic use case: compressing a single file. Open your terminal and navigate to the directory containing the file you want to compress. Then run:

gzip filename.txt

After running this command, you will see that `filename.txt` is gone, and a new file called `filename.txt.gz` appears in its place. The original file is removed automatically. This behavior can be surprising if you are new to Linux, but it is the default action for gzip.

Basic Gzip Command Syntax

The general syntax for gzip is:

gzip [options] filename(s)

You can compress multiple files at once by listing them:

gzip file1.txt file2.txt file3.txt

This creates `file1.txt.gz`, `file2.txt.gz`, and `file3.txt.gz` individually. Gzip does not combine files into a single archive like `tar` does; it compresses each file separately.

Keeping The Original File With -K Or –Keep

If you want to keep the original file after compression, use the `-k` (keep) option:

gzip -k filename.txt

This creates `filename.txt.gz` while leaving `filename.txt` untouched. This is usefull when you need both the compressed and uncompressed versions for different purposes.

Setting Compression Levels With -1 To -9

Gzip offers nine compression levels, from 1 (fastest, least compression) to 9 (slowest, best compression). The default level is 6, which balances speed and size. You can specify a level using a number flag:

gzip -1 filename.txt   # Fastest compression
gzip -9 filename.txt   # Best compression, slowest

Level 1 is great for large files when you need quick results. Level 9 is ideal for archiving where file size matters more than speed. Experiment with both to see what works best for your files.

Compressing All Files In A Directory

To compress every file in the current directory, use a wildcard:

gzip *

This compresses all files (not directories) in the current folder. Each file gets its own `.gz` version. Be careful with this command, as it will replace all regular files with compressed versions.

If you want to compress files recursively through subdirectories, you need to use the `-r` (recursive) flag:

gzip -r myfolder/

This compresses all files inside `myfolder` and its subfolders. Directories themselves are not compressed, only the files within them.

Viewing Compression Details With -L Or –List

Before or after compression, you can check the compression ratio and other details using the `-l` option:

gzip -l filename.txt.gz

This displays the compressed size, uncompressed size, ratio, and the original filename. It is a handy way to verify how much space you saved.

Testing The Integrity Of A .Gz File

To check if a `.gz` file is valid and not corrupted, use the `-t` (test) option:

gzip -t filename.txt.gz

If the file is fine, you will see no output. If there is an error, gzip will display a message. This is usefull before transferring or archiving compressed files.

Decompressing .Gz Files With Gunzip

To decompress a `.gz` file, you can use `gunzip` or `gzip -d`. Both commands work the same way:

gunzip filename.txt.gz
# Or
gzip -d filename.txt.gz

This restores the original file and removes the `.gz` version. To keep the compressed file while decompressing, use the `-k` flag:

gunzip -k filename.txt.gz

Combining Gzip With Tar For Archives

While gzip works on single files, `tar` is used to combine multiple files into one archive. You can then compress that archive with gzip. The most common way is to use the `-z` option with tar:

tar -czvf archive.tar.gz myfolder/

This creates a compressed tar archive called `archive.tar.gz`. The `-z` flag tells tar to use gzip compression. To decompress such an archive, run:

tar -xzvf archive.tar.gz

This combination is so common that many Linux users refer to `.tar.gz` files as “tarballs.” It is the standard way to distribute software and backup directories.

Using Gzip With Pipes

Gzip works well with pipes, allowing you to compress data on the fly. For example, you can compress the output of a command before saving it to a file:

ls -la | gzip > directory_listing.gz

This compresses the directory listing and stores it in a `.gz` file. You can also decompress and pipe data into another command:

gunzip -c directory_listing.gz | less

The `-c` option writes the decompressed data to standard output, which you can then view with `less` or process further.

Compressing With A Custom Output Name

By default, gzip uses the original filename with `.gz` appended. If you want a different name, use the `-c` option and redirect the output:

gzip -c filename.txt > mycustomname.gz

This creates `mycustomname.gz` while leaving `filename.txt` unchanged. This is usefull when you need specific naming conventions for your compressed files.

Checking Gzip Version And Help

To see which version of gzip you are using, run:

gzip --version

For a quick list of all options, use:

gzip --help

Or consult the manual page for detailed documentation:

man gzip

Common Mistakes And How To Avoid Them

One common mistake is forgetting that gzip removes the original file by default. Always use `-k` if you want to keep it. Another issue is trying to compress directories directly. Gzip cannot compress a folder; you must use `tar` first or compress each file individually.

Some users also confuse gzip with zip. Gzip is not the same as the `zip` command. Gzip compresses single files and is often used with tar, while zip can handle multiple files and directories natively. Choose the tool that fits your needs.

If you accidentally compress a file and need it back, use `gunzip` to restore it. But if you deleted the original and the compressed file is corrupted, recovery may be difficult. Always keep backups of important data.

Performance Tips For Large Files

For very large files, compression level 1 is much faster than level 9, though the file size may be slightly larger. If you are compressing logs or temporary files, level 1 is usually sufficient. For long-term storage, level 9 saves more space.

You can also use the `–fast` and `–best` aliases for levels 1 and 9 respectively:

gzip --fast filename.txt
gzip --best filename.txt

These are just shortcuts and do the same thing as `-1` and `-9`.

Integrating Gzip Into Scripts

Gzip is perfect for automation scripts. For example, you can compress log files daily using a cron job:

0 2 * * * gzip /var/log/myapp/*.log

This compresses all log files in the directory at 2 AM every day. Add the `-k` flag if you want to keep the originals for a certain period.

You can also check if a file is already compressed before running gzip to avoid errors:

if [ ! -f "file.txt.gz" ]; then
    gzip file.txt
fi

Alternatives To Gzip

While gzip is widely used, other compression tools like bzip2, xz, and zstd offer different trade-offs. Bzip2 compresses better but is slower. Xz offers even higher compression ratios. Zstd is very fast with good compression. However, gzip remains the most compatible option across systems.

For most everyday tasks, gzip is more than enough. It is pre-installed on almost every Linux distribution, so you do not need to install anything extra.

Frequently Asked Questions

How Do I Gzip A File In Linux Without Losing The Original?

Use the `-k` (keep) option: `gzip -k filename.txt`. This creates a `.gz` file while preserving the original file.

What Is The Difference Between Gzip And Tar?

Gzip compresses a single file, while tar combines multiple files into one archive. They are often used together: `tar -czvf archive.tar.gz folder/` to create a compressed archive.

Can I Gzip A Directory In Linux?

No, gzip cannot compress directories directly. You must use `tar` first to create an archive, then compress it with gzip, or use the `-r` flag to compress files inside the directory individually.

How Do I Check The Compression Ratio Of A Gzip File?

Use `gzip -l filename.txt.gz` to view the compressed size, uncompressed size, and compression ratio.

Is Gzip Lossless Compression?

Yes, gzip uses lossless compression, meaning no data is lost. Decompressing a `.gz` file restores the exact original file.

Now you know how to gzip a file in linux from start to finish. Practice these commands in your terminal to build confidence. Gzip is a simple yet powerful tool that will save you disk space and make file transfers faster. Start compressing today and enjoy the benefits of efficient file management.