Using gzip and gunzip in Linux

When working in Linux, it's common to encounter large files that need to be compressed for storage or transfer. One popular compression tool available in Linux is gzip, which is used to compress files to save disk space and reduce transfer time. In this article, we'll explore how to use gzip and gunzip in Linux, including how to compress and decompress files, and how to work with compressed archives.

What is gzip?

Gzip is a compression utility tool used in Linux and other Unix-based systems. It's used to compress and decompress files, reducing their size for storage and transfer. Gzip works by replacing repeated strings of data in a file with a shorter representation. When the file is decompressed, the original data is reconstructed from the shorter representation.

Basic Usage

To compress a file using gzip, you can use the following command ?

gzip file.txt

This command will compress the file named file.txt and create a new file named file.txt.gz. The original file will be deleted, and the compressed file will be saved in its place.

To decompress a file that has been compressed with gzip, you can use the following command ?

gunzip file.txt.gz

This command will decompress the file file.txt.gz and create a new file named file.txt. The compressed file will be deleted, and the decompressed file will be saved in its place.

Common Options

Gzip provides several options that can be used to modify its behavior. Here are some commonly used options ?

Option Description Example
-c Write output to stdout, keep original file gzip -c file.txt > compressed.gz
-d Decompress (same as gunzip) gzip -d file.txt.gz
-f Force compression/decompression gzip -f file.txt
-k Keep original file during compression gzip -k file.txt
-r Compress files recursively in directory gzip -r directory/
-v Verbose output (show compression ratio) gzip -v file.txt

Examples with Options

# Compress while keeping original file
gzip -k file.txt

# Compress all text files
gzip *.txt

# Compress with verbose output
gzip -v document.pdf

# Force compression even if file exists
gzip -f backup.log

Working with Archives

Gzip is commonly used with tar to create compressed archives. A compressed archive is a collection of files and directories that have been compressed into a single file for easier storage and transfer.

To create a compressed archive with gzip ?

tar czf archive.tar.gz directory/

This command creates a new compressed archive named archive.tar.gz that contains all files and directories in the directory/. The c option tells tar to create a new archive, z option tells tar to use gzip compression, and f option specifies the name of the output file.

To extract a compressed archive created with gzip ?

tar xzf archive.tar.gz

Add the -v option to see progress during archive operations ?

tar czvf archive.tar.gz directory/
tar xzvf archive.tar.gz

Advanced Usage

Using gzip with Pipes

You can use gzip with pipes to compress data directly from standard input ?

cat file.txt | gzip > file.txt.gz
echo "Hello World" | gzip > message.gz

Checking Compression Information

Use -l option to list information about compressed files ?

gzip -l file.txt.gz
         compressed        uncompressed  ratio uncompressed_name
               1024                2048  50.0% file.txt

Setting Compression Level

You can specify compression levels from 1 (fastest) to 9 (best compression) ?

gzip -1 file.txt  # Fast compression
gzip -9 file.txt  # Maximum compression

Gunzip Advanced Options

The gunzip command supports similar options for decompression ?

# Decompress multiple files
gunzip *.gz

# Decompress while keeping compressed file
gunzip -k file.txt.gz

# Test compressed file integrity
gunzip -t file.txt.gz

# Force decompression
gunzip -f file.txt.gz

Practical Examples

# Compress log files older than 7 days
find /var/log -name "*.log" -mtime +7 -exec gzip {} \;

# Create backup with compression
tar czf backup-$(date +%Y%m%d).tar.gz /home/user/documents

# Decompress and view file content
gunzip -c file.txt.gz | less

Conclusion

Using gzip and gunzip in Linux is a simple and effective way to compress and decompress files and archives. These tools provide excellent compression ratios and integrate seamlessly with other Linux utilities. Whether you're managing disk space, creating backups, or transferring files, gzip offers reliable compression capabilities for your Linux workflow.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements