How To Compress A File In Linux

How To Compress A File In Linux

Reading time1 min
#Linux#Compression#File#Tar#Gzip#Zstd

Mastering File Compression in Linux: Beyond the Basics with tar, gzip, and Advanced Tools

Most guides stop at "gzip this" or "tar that." Let's break free from the basics and explore when and why to use each compression tool—and reveal lesser-known methods that can cut your file sizes even further.

Efficient file compression isn’t just a nice-to-have skill; it’s essential for Linux users managing servers, backups, or large datasets. Whether you’re freeing up storage space or preparing data for transfer over slow networks, knowing how to compress files effectively can save time and resources. In this post, I’ll walk you through mastering file compression on Linux—from traditional commands like tar and gzip to advanced tools that offer superior compression ratios.


Why Compress Files in Linux?

Before we dive into commands, let’s quickly recap why compressing files is so valuable:

  • Save disk space: Large log files, backups, or databases can quickly consume storage.
  • Speed up transfers: Smaller files send faster over the network—critical for remote backups or cloud sync.
  • Organize related files: Archiving multiple files/directories into a compressed single archive simplifies management.

The Classic Trio: tar, gzip, and bzip2

1. Using tar — The Archiver

tar itself isn’t a compressor; it’s an archiving tool that bundles multiple files/folders into one .tar archive.

Basic syntax:

tar -cvf archive.tar /path/to/folder
  • c: create archive
  • v: verbose (lists files)
  • f: filename (the archive file)

2. Compress with gzip — Fast and Popular

To compress your tarball, you usually pipeline with gzip or use shorthand:

tar -czvf archive.tar.gz /path/to/folder
  • z: filter through gzip compression

This creates a .tar.gz file—commonly known as a "tarball."

Extracting:

tar -xzvf archive.tar.gz

or decompress gzip-only files with:

gunzip file.gz

3. bzip2 — Better Compression but Slower

If you want better compression at the cost of speed:

tar -cjvf archive.tar.bz2 /path/to/folder
  • j: filter through bzip2

Extract with:

tar -xjvf archive.tar.bz2

When to Choose Each Traditional Tool?

ToolSpeedCompression RatioUse Case
gzipFastModerateQuick compression with decent savings
bzip2SlowerBetterWhen disk space trumps CPU time
xzSlow (especially)BestMax compression on archives

Go Further: Advanced Compression Tools on Linux

The modern landscape offers some impressive alternatives you should know about.

1. xz — High Compression Ratio with LZMA2 Algorithm

Syntax is similar to tar + bz2 but often yields smaller files:

tar -cJvf archive.tar.xz /path/to/folder
  • J: filter through xz

Extract:

tar -xJvf archive.tar.xz

When to use?

Use xz for large backups where maximum space saving outweighs compression time.

2. zstd (Zstandard) — Balance of Speed and Compression

Zstd offers ultra-fast compression with ratios comparable to xz or bzip2.

Install it first if not present:

sudo apt install zstd # Debian/Ubuntu
sudo yum install zstd # RHEL/CentOS/Fedora 

Create compressed tarball archives:

tar --use-compress-program=zstd -cvf archive.tar.zst /path/to/folder

Extract:

tar --use-compress-program=zstd -xvf archive.tar.zst

You can also compress single files:

zstd file.txt        # creates file.txt.zst by default
unzstd file.txt.zst  # decompresses back to original 

3. lz4 — Ultra-fast Compression (Less Focused on Size)

Best suited when speed is critical more than small size.

Compress single file

lz4 bigfile.log bigfile.log.lz4
lz4 -d bigfile.log.lz4 bigfile.log   # decompress 

Practical How-To: Compress a Directory Efficiently

Let’s say you want to back up /var/www/html directory.

Step 1: Check directory size first

du -sh /var/www/html/

Step 2: Compress with gzip (standard option)

tar -czvf www-backup.tar.gz /var/www/html/

Check resulting size:

ls -lh www-backup.tar.gz 

Step 3: Try xz for better size saving

tar -cJvf www-backup.tar.xz /var/www/html/
ls -lh www-backup.tar.xz 

Step 4: Try zstd for balanced speed + size

tar --use-compress-program=zstd -cvf www-backup.tar.zst /var/www/html/
ls -lh www-backup.tar.zst 

Most likely, you’ll find zstd takes much less time than xz but still smaller than gzip!


Tips & Tricks for Mastering Compression in Linux

  • Multi-threading: Tools like pigz, an alternative to gzip that supports multiple CPU cores:

    pigz file.txt  
    
  • Compression level: Most tools support levels from fast & low compression (-1) up to slow & high (-9, range varies):

    gzip -9 file.txt  
    zstd -19 file.txt  
    
  • Encrypt on the fly: Combine compression with encryption via gpg:

    tar -czf - directory/ | gpg --symmetric > directory.tar.gz.gpg  
    
  • Test integrity: Always verify your archives after compression/extraction:

    tar --diff -zf your-archive.tar.gz    # checksum differencing   
    

Conclusion: Choose Your Compressor Wisely

Mastering Linux file compression means more than memorizing commands—it requires understanding use cases and available tools so you can choose the best one every time.

ScenarioRecommended Tool
Quick everyday compressgzip, possibly multi-threaded pigz
Maximum space savingxz, possibly combined with multi-threading tools like pxz
Balanced speed & ratiozstd, increasingly becoming default choice
Ultra fast (less squeezing)lz4

Armed with these tools and insights, you’re ready to optimize your Linux workflows—whether managing server logs, backups, codebases, or huge data sets.

Happy compressing! 🚀


P.S. Have questions or want me to cover specific real-world examples? Drop them in the comments below!