Overview
The gzip and gunzip commands are the standard tools for handling the .gz file format in Linux. gzip is used to compress files, and gunzip is used to decompress (extract) them. It is important to note that unlike the ZIP format in Windows, these commands do not have the ability to “bundle” multiple files into a single archive. If you need to compress an entire directory into one file, you must use the tar command instead.
Specifications (Arguments and Options)
Syntax
gzip [options] filename
gunzip [options] filename
Note: gunzip is exactly the same as gzip -d.
Main Arguments and Options
| Option | Description |
-c / --stdout | Writes the result to standard output. Use this if you want to keep the original file. |
-d / --decompress | Decompresses the file (same as gunzip). |
-f / --force | Forces compression or overwriting if a file with the same name already exists. |
-l / --list | Displays the compression ratio and the size of the original file. |
-r / --recursive | Processes all files inside a directory individually. |
-S <suffix> | Changes the file extension from .gz to a custom string. |
-1 to -9 | Sets the compression level. -1 is the fastest (low compression), while -9 is the highest (slowest). The default is -6. |
Basic Usage
By default, the gzip command replaces the original file with a new file ending in .gz. When you decompress it, the .gz file is replaced by the original file.
Command
# Create a test file
touch app.log
# Compress the file (app.log is replaced by app.log.gz)
gzip app.log
# Verify the file
ls -l app.log*
# Decompress the file (app.log.gz is replaced by app.log)
gunzip app.log.gz
Execution Result
-rw-r--r-- 1 user user 35 Jan 20 12:00 app.log.gz
Practical Commands
Compress All Files Inside a Directory Recursively
Using the -r option tells the command to go through all folders and compress every file it finds “individually.” This does not combine the files into a single archive file.
# Compress all files inside the logs directory individually
gzip -r ./logs/
Check Information for a Compressed File
You can see the original file size and the compression ratio without having to decompress the file first.
# Display compression information
gzip -l app.log.gz
Example Output:
compressed uncompressed ratio uncompressed_name
65 120 48.3% app.log
Compare Sizes Using Different Compression Levels
You can test the trade-off between compression speed (-1) and file size (-9). The default level is -6.
# Copy a file for testing
cp /bin/ls test_file
# Fast compression (Level 1)
cp test_file test_1 && gzip -1 test_1
# Highest compression (Level 9)
cp test_file test_9 && gzip -9 test_9
# Compare the resulting file sizes
ls -l test_*
Compress or Decompress While Keeping the Original File
By using the -c option to send the result to standard output and redirecting it to a new file, you can keep the source file intact.
# Decompress while keeping the .gz file
gzip -dc data.csv.gz > data_restored.csv
# Compress while keeping the original text file
gzip -c config.txt > config.txt.gz
Customization Points
- Changing Extensions: You can change the default
.gzextension using-S. However, this is not recommended as it makes file management more difficult. - Viewing Text Files: To quickly read the content of a compressed text file without decompressing it, use the
zcatorzlesscommands.
Important Notes
- Loss of Original Files: As mentioned,
gzipdeletes the source file after compression. Always use the-coption with redirection if you need to keep the original file. - No Archiving Feature: Running
gzipon a directory name will result in an error. To compress an entire folder into one file, usetar(e.g.,tar czf dir.tar.gz dir/). - Overwriting: If a file with the same name exists at the destination, the command will ask for confirmation. Use
-fto force the action if you are using this in a script.
Applications
Automatically Compress Old Log Files
You can search for specific files, such as log files that haven’t been modified in over 30 days, and compress them to save disk space.
# Find and compress all .log files modified more than 30 days ago
find /var/log/app -name "*.log" -mtime +30 -exec gzip {} \;
Summary
The gzip and gunzip commands are fundamental for managing file compression in Linux. They are specifically designed to reduce the size of individual files and are frequently used for log rotation and data transfer tasks. By understanding that these commands replace original files and do not archive directories, you can effectively use them as part of your server management toolkit.
