LinuxTutorial2 min read

How to Create and Extract ZIP and TAR Files on Linux

Compress and extract files on Linux using zip, unzip, and tar — with clear examples for creating archives, extracting them, and choosing the right format.

Developer terminal on a laptop in low light

ZIP vs TAR — What's the Difference?

ZIP is the universal format for cross-platform sharing. TAR (Tape ARchive) bundles files together without compression; pair it with gzip (.tar.gz) or bzip2 (.tar.bz2) to compress. On Linux, tar.gz (often called tarball) is the most common format for source code and backups.

ZIP Files

Create a ZIP archive

zip archive.zip file1.txt file2.txt    # zip specific files
zip -r project.zip project/            # zip a directory (-r = recursive)
zip -r -9 project.zip project/         # maximum compression level

Extract a ZIP archive

unzip archive.zip                      # extract to current directory
unzip archive.zip -d /tmp/output/      # extract to a specific directory
unzip -l archive.zip                   # list contents without extracting

Install zip/unzip if missing

sudo apt install zip unzip

TAR Files

The tar flags to remember:

  • c — create
  • x — extract
  • z — use gzip compression (.gz)
  • j — use bzip2 compression (.bz2)
  • f — specify the filename (always last before the filename)
  • v — verbose, print files as they're processed

Create a tar.gz archive

tar -czf archive.tar.gz directory/         # compress a directory
tar -czf backup.tar.gz file1.txt file2.txt # compress specific files
tar -czvf archive.tar.gz directory/        # verbose mode

Extract a tar.gz archive

tar -xzf archive.tar.gz                    # extract to current directory
tar -xzf archive.tar.gz -C /tmp/output/    # extract to specific directory
tar -xzvf archive.tar.gz                   # verbose extraction

List contents without extracting

tar -tzf archive.tar.gz

bzip2 archives (.tar.bz2)

tar -cjf archive.tar.bz2 directory/   # create
tar -xjf archive.tar.bz2              # extract

Quick Reference

# Create
zip -r out.zip folder/
tar -czf out.tar.gz folder/

# Extract
unzip out.zip
tar -xzf out.tar.gz

# Peek inside
unzip -l out.zip
tar -tzf out.tar.gz

Which Format to Use?

  • Sharing with Windows/Mac users → ZIP
  • Linux server backups and source code → tar.gz
  • Maximum compression, slow speed → tar.bz2 or tar.xz