DEV Community

Sreekanth Kuruba
Sreekanth Kuruba

Posted on

File Compression in Linux Explained Simply (tar, gzip, zip & unzip)

Working with files in Linux isn't just about creating and editing them.

Sometimes you need to:

  • Archive multiple files into one
  • Compress files to save disk space
  • Share files with others
  • Create backups

Linux provides several tools for this, each with a different purpose.

Let's simplify them.


What is File Compression?

File compression reduces the size of a file.

Benefits:

  • Saves disk space
  • Faster file transfers
  • Easier backups
  • Reduces bandwidth usage

Example:

A 100 MB log file might become a much smaller compressed file, depending on its contents.


Archive vs Compression

Many beginners think they're the same.

They are not.

Archive

Combines multiple files into a single file.

Example:

photos/
docs/
notes.txt

↓

backup.tar
Enter fullscreen mode Exit fullscreen mode

Compression

Reduces the size of a file.

Example:

backup.tar

↓

backup.tar.gz
Enter fullscreen mode Exit fullscreen mode

👉 tar archives files. gzip compresses them.


1. Create an Archive with tar

tar -cvf backup.tar Documents/    #Create an archive
tar -tvf backup.tar               #View archive contents
tar -xvf backup.tar               #Extract an archive
Enter fullscreen mode Exit fullscreen mode

Options:

  • c → Create
  • v → Verbose (show progress)
  • f → File name
  • x → Extract

Best for:

  • Backups
  • Bundling multiple files
  • Moving folders

2. Compress with gzip

Compress a file:

gzip file.txt          # Creates file.txt.gz
# Result
file.txt.gz
gunzip file.txt.gz     # decompress
gzip -k file.txt       # Keep original file
Enter fullscreen mode Exit fullscreen mode

Best for:

  • Log files
  • Large text files
  • Saving disk space

3. Archive and Compress Together

Most common command:

# Create compressed archive
tar -czvf backup.tar.gz Documents/
# Extract
tar -xzvf backup.tar.gz
Enter fullscreen mode Exit fullscreen mode

Options:

  • z → Use gzip compression

👉 This is one of the most common backup commands in Linux.


4. Working with ZIP Files

# Create ZIP
zip -r project.zip project/
# Extract
unzip project.zip
# List contents
unzip -l project.zip
Enter fullscreen mode Exit fullscreen mode

Best for:

  • Sharing files with Windows users
  • Cross-platform compatibility

5. Compare the Tools

Tool Purpose Best For
tar Archive files Backups
gzip Compress files Saving space
tar + gzip Archive and compress Linux backups
zip Archive and compress Sharing files across operating systems

Real-World Example

You need to back up a website before making changes.

# Create backup
tar -czvf website-backup.tar.gz /var/www/html
# Restore later if needed
tar -xzvf website-backup.tar.gz
Enter fullscreen mode Exit fullscreen mode

This is a common practice before upgrades, migrations, or configuration changes.


Common Beginner Mistakes

  • Thinking tar compresses files by itself
  • Forgetting -z when creating .tar.gz archives
  • Compressing files that are already compressed (like .jpg or .mp4)
  • Extracting archives into the wrong directory
  • Deleting backups before verifying they can be restored

Simple Mental Model

Think of it like packing for a trip:

  • tar → Put all your clothes into one suitcase.
  • gzip → Compress the suitcase to make it smaller.
  • tar.gz → Pack everything into one suitcase and compress it.
  • zip → A suitcase that's easy to share with almost any operating system.

Summary

In this guide you learned:

  • What file compression is
  • Archive vs. compression
  • Creating archives with tar
  • Compressing files with gzip
  • Creating .tar.gz archives
  • Working with ZIP files
  • Common beginner mistakes

Why This Matters

File compression is used every day by:

  • Linux administrators
  • DevOps engineers
  • Cloud engineers
  • Developers

Whether you're creating backups, transferring files, or saving disk space, understanding these tools is an essential Linux skill.


Congratulations!
You’ve now covered the essential Linux fundamentals.

In the next and final post, we’ll combine everything from this series into a practical troubleshooting workflow you can use to diagnose Linux issues with confidence.

Next Post

Linux Troubleshooting Flow for Beginners


Question for You

Which compression format do you use most often: .tar.gz or .zip? And in what situation?


Top comments (0)