How to Use the Gzip Command in Linux

Gzip's combination of good compression ratio, speed, and widespread availability makes it one of the most versatile compression tools in the Linux environment.

Alby Andersen

The gzip command in Linux is used to compress files using the Lempel-Ziv (LZ77) algorithm, reducing their size for efficient storage and transfer. It replaces the original file with a compressed version (.gz extension) by default. gzip is ideal for compressing individual files, while directories or multiple files are often combined with tar first.

Key features include adjustable compression levels, integrity checks, and integration with tools like gunzip (for decompression) and zcat (for viewing compressed files).

Basic Usage Examples

Compress a Single File

gzip filename.txt

This compresses the file and replaces it with filename.txt.gz. The original file is removed.

Compress While Keeping the Original File

gzip -c filename.txt > filename.txt.gz

The -c option writes to standard output and keeps the original file intact.

Decompress a Gzipped File

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

This will extract the file and remove the compressed version.

View Contents Without Decompressing

zcat filename.txt.gz
# Or for paging through content:
zless filename.txt.gz
zmore filename.txt.gz

These commands let you view the content of compressed files without extracting them.

Checking Compression Information

gzip -l filename.txt.gz

Displays information about the compressed file, including compression ratio.

Compress and Preserve Timestamps

gzip -N filename.txt  

-N retains the original file’s modification time.

Advanced Usage

Adjusting Compression Levels

# Maximum compression (slower)
gzip -9 largefile.log

# Fastest compression (less compression)
gzip -1 largefile.log

# Default compression level
gzip -6 largefile.log

Compression levels range from 1 (fastest, least compression) to 9 (slowest, maximum compression).

Recursive Compression

gzip -r directory/

Recursively compresses all files in a directory and its subdirectories.

Compressing Multiple Files

gzip file1.txt file2.txt file3.txt

Creates separate compressed files: file1.txt.gz, file2.txt.gz, and file3.txt.gz.

Testing Integrity of Compressed Files

gzip -t archive.gz

Tests the integrity of the compressed file without decompressing it.

Force Compression Regardless of Size

gzip -f smallfile.txt

Forces compression even if the compressed file would be larger than the original.

Working with Standard Input/Output

# Compress output of a command
ls -la | gzip > directory_listing.gz

# Decompress and pipe to another command
zcat logfile.gz | grep "error"

Gzip works well with pipes for processing command output.

Controlling Verbosity

gzip -v largefile.log

Shows the compression ratio and other information during compression.

Combine with tar for Directories

tar -czvf archive.tar.gz /path/to/directory  

tar bundles the directory, and gzip (via -z) compresses the archive.

Practical Use Cases

Compressing Log Files

# Compress yesterday's log
gzip access.log.1

# Rotate and compress logs
logrotate -f /etc/logrotate.conf

Log files are excellent candidates for compression since they’re rarely accessed once archived.

Compressing Files for Transfer

# Create and compress a tarball
tar -czf archive.tar.gz directory/

# Sending compressed data over network
ssh user@remote "cat > file.gz" < file.gz

Compression reduces transfer time over networks.

Handling Large Datasets

# Process a large compressed CSV file
zcat large_dataset.csv.gz | awk -F, '{sum+=$3} END {print sum}'

Process large files without needing disk space for decompression.

Performance Considerations

Using Multi-threaded Compression (pigz)

# Install pigz (parallel implementation of gzip)
sudo apt install pigz  # For Debian/Ubuntu

# Use all available cores
pigz largefile.log

# Specify number of threads
pigz -p 4 largefile.log

For faster compression of large files on multi-core systems.

Finding the Best Compression Balance

# Benchmark different compression levels
time gzip -1 largefile.log
time gzip -6 largefile.log
time gzip -9 largefile.log

Finding the right balance between compression time and ratio.

Integration with Other Tools

Using with tar for Directory Compression

# Create compressed archive
tar -czf archive.tar.gz directory/

# Extract compressed archive
tar -xzf archive.tar.gz

The z flag tells tar to use gzip compression/decompression.

Using with find to Compress Old Files

find /var/log -name "*.log" -mtime +30 -exec gzip {} \;

Automatically compress log files older than 30 days.

Combining with rsync for Efficient Transfers

rsync -az source_dir/ user@remote:/destination/

The z flag with rsync enables compression during transfer.

Share This Article