Introduction
In the day-to-day work of system administrators and developers, it is often necessary to reduce file size to save disk space, speed up transfers, or create lighter backups. The gzip command is one of the simplest and most effective tools available in almost all Linux distributions to achieve this compression without losing data.
What is gzip?
Gzip stands for GNU zip and is a compression program that uses the DEFLATE algorithm, the same one found in the ZIP format. Unlike tools like tar, gzip works on a single file at a time, although it is often combined with tar to bundle multiple files before compressing them.
Basic Syntax
The simplest way to use gzip is:
gzip filename
This command creates a compressed file named filename.gz and deletes the original, unless otherwise indicated with the -k option.
Most Useful Options
- -k or –keep: keeps the original file after compression.
- -d or –decompress: decompresses a .gz file and returns the original file.
- -level: indicates the compression level from 1 (fastest, least compression) to 9 (maximum compression, slowest). By default level 6 is used.
- -v or –verbose: shows detailed information about the compression ratio and time used.
- -l or –list: shows statistics for one or several compressed files without decompressing them.
- -f or –force: forces compression even if the .gz file already exists or if it is a symbolic link.
Practical Examples
- Compress a log file while keeping the original:
gzip -k server.log - Decompress a file and see the process:
gzip -dv file.txt.gz - Apply maximum compression to a backup:
gzip -9 -k backup.sql - View the compression ratio of several files:
gzip -l *.gz - Force recompression of an already compressed file:
gzip -f -k file.txt.gz
Decompressing with gzip
To restore a compressed file, simply use the -d option or call gunzip directly, which is a symbolic link to gzip with decompression enabled by default:
gzip -d file.gz
or:
gunzip file.gz
Both commands will leave the original file without the .gz extension and delete the compressed file, unless -k is used to keep it.
Tips and Best Practices
- Combine gzip with tar to create .tar.gz files, which are the standard for distributing source code and packages in Linux.
- Use low compression levels (1-3) when you need speed, for example in real-time processing pipelines.
- Reserve level 9 for files that will be stored for long periods where compression time is not critical.
- Always verify the integrity of compressed files with
gzip -tbefore deleting them. - In backup scripts, add the -k option to avoid accidental loss of the original file in case of failures.
Conclusion
The gzip command remains a fundamental piece in the toolbox of any Linux user. Its simplicity, efficiency, and broad support make it ideal for everyday compression and decompression tasks. Knowing its options and when to apply them will allow you to optimize space usage and improve the speed of your daily operations.
This post is also available in ESPAÑOL.