How to Zip a File in Linux?

File compression is a fundamental task in Linux system administration for managing storage space and efficiently transferring files. Linux provides several powerful command-line tools including gzip, zip, tar, and others for compressing files and directories. This guide covers the most commonly used compression methods and their practical applications.

Using the gzip Command

gzip (GNU zip) is a widely-used command for single file compression in Linux. It provides excellent compression ratios and is included by default in most distributions.

gzip filename.txt

This command compresses filename.txt and renames it to filename.txt.gz. The original file is replaced with the compressed version. gzip only works on individual files for directories, combine it with tar.

Compression Levels

gzip supports compression levels from 1 (fastest) to 9 (maximum compression). The default is level 6.

gzip -9 filename.txt

Using tar with gzip

The tar (tape archiver) command bundles multiple files and directories into a single archive, which can then be compressed with gzip.

tar -czvf archive_name.tar.gz directory_name/

The flags used are

  • c Create a new archive

  • z Compress with gzip

  • v Verbose output

  • f Specify archive filename

Excluding Files

You can exclude specific files or patterns during compression

tar -czvf archive_name.tar.gz --exclude='*.log' directory_name/

Using the zip Command

The zip command creates cross-platform compatible archives. Install it if not available

sudo apt-get install zip    # Ubuntu/Debian
sudo yum install zip        # CentOS/RHEL

Basic Usage

zip archive_name.zip filename.txt
zip -r archive_name.zip directory_name/

The -r flag recursively includes all subdirectories and files.

Password Protection

zip -e secure.zip file.txt

This prompts for a password to encrypt the archive contents.

Managing Zip Archives

zip archive_name.zip file_to_add.txt     # Add file
zip -d archive_name.zip file_to_remove.txt   # Remove file
unzip -l archive_name.zip                # List contents

Using bzip2 Command

bzip2 provides better compression ratios than gzip but is slower. It works similarly to gzip for single files

bzip2 filename.txt                        # Compress
bunzip2 filename.txt.bz2                  # Decompress
tar -cjvf archive_name.tar.bz2 directory_name/  # With tar

The j flag in tar specifies bzip2 compression.

Comparison of Compression Methods

Method Extension Compression Speed Compatibility
gzip .gz, .tar.gz Good Fast Unix/Linux
bzip2 .bz2, .tar.bz2 Better Slower Unix/Linux
zip .zip Good Fast Cross-platform
7zip .7z Best Slower Cross-platform

Extracting Archives

Different archive types require specific extraction commands

gunzip filename.txt.gz                   # Extract gzip
tar -xzvf archive_name.tar.gz            # Extract tar.gz
tar -xjvf archive_name.tar.bz2           # Extract tar.bz2
unzip archive_name.zip                   # Extract zip
7z x archive.7z                          # Extract 7z

The x flag in tar means extract, and you can specify a destination with -C directory.

Advanced Usage

Piping Commands

Combine multiple files into a compressed archive using pipes

tar -cf - file1.txt file2.txt | gzip > files.tar.gz

Viewing Archive Contents

tar -tf archive.tar.gz                   # List tar contents
unzip -l archive.zip                     # List zip contents
7z l archive.7z                          # List 7z contents

Conclusion

Linux offers multiple compression tools, each with specific strengths. gzip and tar are ideal for Unix environments, zip provides cross-platform compatibility, while bzip2 and 7zip offer superior compression ratios. Choose the appropriate method based on your compatibility needs, compression requirements, and performance considerations.

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

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements