Introduction
In the day-to-day of system administrators and developers, you often need to reduce file size to save disk space, accelerate transfers or create lighter backup. 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 means GNU zip and is a compression program that uses the DEFLATE algorithm, the same as in the ZIP format. Unlike tools such as tar, gzip works on one single file at a time, although it is usually combined with tar to pack multiple files before compressing them.
Basic syntax
The simplest way to use gzip is:
gzip name _ of _ file
This command creates a compressed file called _ filen.gz _ name and removes the original, unless otherwise indicated with the -k option.
Most useful options
- -k or -keep: store the original file after compression.
- -d or -decompress: decompress a .gz file and return the original file.
- -level: indicates the level of compression from 1 (faster, less compression) to 9 (maximum compression, slower). By default level 6 is used.
- -v or -verbose: shows detailed information on the compression ratio and time used.
- -l or -list: shows statistics of one or more compressed files without uncompressing them.
- -f or -force: forces the compression even if the .gz file already exists or if it is a symbolic link.
Practical examples
- Compress a log log keeping the original:
gzip -k servidor.log - Uncompress a file and view the process:
gzip -dv archivo.txt.gz - Apply maximum compression to a backup:
gzip -9 -k backup.sql - See the compression ratio of several files:
gzip -l *.gz - Forging the recharge of an already compressed file:
gzip -f -k archivo.txt.gz
Uncompress with gzip
To restore a compressed file simply use the -d option or call directly to gunzip, which is a symbolic link to gzip with the default activated decompression:
gzip -d archivo.gz
or:
gunzip archivo.gz
Both commands will leave the original file without the .gz extension and remove the compressed file unless it is used -k to keep it.
Tips and good practices
- Combine gzip with tar to create .tar.gz files, which are 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.
- Book level 9 for files that will be stored for long periods and where the compression time is not critical.
- Always check the integrity of the compressed files with
gzip -tbefore you take them out. - 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 key piece in any Linux user's toolbox. Its simplicity, efficiency and broad support make it ideal for daily compression and decompression tasks. Knowing your options and knowing when to apply them will allow you to optimize the use of space and improve the speed of your daily operations.


