How to Archive, Compress, and Extract Files on Linux with Gzip, Zip, and Tar

Learn how to archive, compress and extract files in Linux using gzip, zip, and tar. Save space, speed up file transfers, and keep your data organized.

Publish date: 2/2/2025

In Linux environments, gzip and tar have long been go-to utilities for file compression and archiving, while the zip format—though originally developed outside Unix—has evolved into a cross-platform mainstay.

Each excels at different tasks, from straightforward compression to bundling directories with permissions intact, or sharing files across operating systems.

So how do you use each one effectively on Linux, and which scenarios do they handle best?

Well, that's what we aim to answer in this article. In it, we’ll walk you through all of the three utilities, highlight their strengths, and show you how to use them accordingly. Let's dive in.

Gzip and zip explained

Compressing files with gzip

gzip is a popular compression tool that comes preinstalled on most Linux distributions. By convention, files compressed with gzip end in .gz.

To compress a file:

gzip filename.ext

This creates filename.ext.gz.

To decompress and return the original file:

gunzip filename.ext.gz

Afterward, filename.ext is restored, and the compressed file is removed.

Compressing files with zip

zip is especially handy when sharing files across different operating systems. To create a zip archive:

zip -r filename.zip files

Replace filename.zip with your preferred name, followed by the files or directories you want included.

To decompress a zipped file:

unzip filename.zip

(You can include multiple files or directories by listing them all in the command.)

Are gzip and zip different?

Yes. While gzip and zip might seem similar at first glance, they're designed to solve different problems.

gzip is like a specialized tool that does one thing really well: it compresses a single file at a time and keeps track of important Linux system details like file permissions and timestamps. It's perfect for server tasks like shrinking large log files or database backups.

zip, on the other hand, is more like a Swiss Army knife—it can compress multiple files into one archive, lets you add or extract individual files without unpacking everything, and even includes password protection. This flexibility, combined with the fact that practically every operating system understands zip files, makes it the ideal choice when sharing files across different platforms. That's why you'll often see software distributed as .zip files for Windows and Mac users, while Linux systems typically use .tar.gz (gzip) for their own backups and packages.

What if you get a "command not found" error?

On some Linux distributions, gzip, zip, or even unzip might not be installed by default. If you run into a “command not found” or similar error, just install them:

Debian/Ubuntu-based systems:

sudo apt update
sudo apt install gzip zip unzip

Fedora/CentOS/Red Hat-based systems:

sudo dnf install gzip zip unzip

(Or sudo yum install gzip zip unzip on older versions.)

This ensures you have all the commands you need for compression and decompression.

Tar for archiving

tar (short for “Tape Archive”) is primarily an archiving tool that bundles multiple files or directories into a single file (often called a “tarball”). By itself, tar doesn’t perform compression—it just groups files. However, it can integrate with other compression tools like gzip or bzip2 to create compressed archives.

Creating a tar file

tar -cvf filename.tar files/directories

This creates filename.tar from the specified files or directories.

To view its contents:

tar -tvf filename.tar

To extract:

tar -xvf filename.tar

You’ll still have filename.tar intact, plus the extracted files in your current directory.

Compressing tar files with gzip

By adding -z, you can compress the archive with gzip in one go:

tar -czvf filename.tgz files/directories

Likewise, decompress and extract with:

tar -xzvf filename.tgz

You’ll end up with the original directory structure restored in your working directory.

Choosing the right tool

  1. gzip
  • Pros: Fast, widely available, ideal for compressing single files.
  • Cons: Limited options for archiving multiple files (you’d typically bundle them with tar first).
  • Use case: When you need quick compression/decompression for a single large file (e.g., logs, database dumps).
  1. zip
  • Pros: Cross-platform compatibility, straightforward archiving and unarchiving, optional passwords.
  • Cons: Typically lower compression ratio compared to advanced algorithms (like xz or bzip2).
  • Use case: Best when sharing files with Windows or macOS users; .zip is almost universally recognized.
  1. tar (+ gzip)
  • Pros: Efficiently archives many files while applying solid compression; flexible usage with other algorithms (bzip2, xz).
  • Cons: Slightly more complex syntax; not inherently cross-platform, although most modern systems support .tar.gz.
  • Use case: Ideal for bundling entire directories (like /etc/home, or project folders) into a single compressed file for backups or deployments.

Want to learn more about hardening or optimizing your Linux environment? Check out our guide on How to Protect Your Linux Server from Brute Force Attacks with Fail2Ban, or explore our blog for additional Linux and self-hosting topics.

Conclusion

File compression and archival utilities are essential building blocks in Linux, and using them effectively is a crucial skill for anyone managing systems.

gzip, zip, and tar each shine in different scenarios. gzip excels at streaming compression of individual files, zip makes cross-platform sharing seamless, and tar with compression handles complex system backups while preserving all the Unix filesystem details that matter.

The best part? Once you understand their strengths, you'll instinctively know which one to reach for. Whether you're rotating massive log files, sharing data with Windows users, backing up entire directory structures, or beyond, you'll have the right tool for the job.

Looking for reliable infrastructure?

xTom provides everything from dedicated servers and colocation to high-performance NVMe VPS through V.PS and more. We'd love to help, just reach out :-)!