Quick take: Use zip -r archive.zip folder to create a ZIP and unzip archive.zip to extract it. ZIP is the cross-platform choice when sharing with Windows and macOS users, where tar.gz is less convenient.

Introduction

While Linux favours tar.gz, the ZIP format is the universal choice for sharing files with Windows and macOS users. The zip command creates archives and unzip extracts them, both with simple, memorable options for compressing folders, protecting archives, and previewing contents.

This guide covers creating and extracting ZIP archives, recursion for folders, password protection, and listing an archive before extracting.

Syntax

The basic syntax of the zip and unzip command is:

zip [OPTIONS] archive.zip files...
unzip [OPTIONS] archive.zip

Common Options and Parameters

The most useful options and parameters for the zip and unzip command:

OptionDescription
zip -rRecursively include a folder and its contents.
zip -eEncrypt the archive with a password.
zip -9Maximum compression (slower).
zip -x PATExclude files matching a pattern.
unzip -lList the contents without extracting.
unzip -d DIRExtract into a specific directory.
unzip -oOverwrite existing files without prompting.
unzip -P PASSSupply a password for an encrypted archive.

Practical Examples

Real zip and unzip commands you can run today:

# Zip a single file
zip backup.zip report.txt
# Zip a folder recursively
zip -r site.zip ./mysite
# Create a password-protected archive
zip -e -r secure.zip ./private
# Exclude a subfolder while zipping
zip -r app.zip app/ -x 'app/node_modules/*'
# List an archive's contents
unzip -l site.zip
# Extract into a specific directory
unzip site.zip -d /var/www/

Tips and Best Practices

  • Use zip -r for folders — without -r the folder is added but not its contents.
  • Preview an unknown archive with unzip -l before extracting so you know what (and where) it will create.
  • ZIP encryption (-e) is convenient but weak; for sensitive data prefer a tar.gz encrypted with GPG.

Final Thoughts

zip and unzip handle the cross-platform archive format everyone can open. Remember -r for folders, unzip -l to preview, and -d to extract to a chosen directory. For Linux-to-Linux transfers, tar.gz is usually better; reach for ZIP whenever Windows or macOS users are involved.

FAQ: zip and unzip Command in Linux

How do I create a zip file in Linux?+

Use zip archive.zip file1 file2 for files, or zip -r archive.zip folder to include a folder and everything inside it recursively.

How do I unzip a file in Linux?+

Run unzip archive.zip to extract into the current directory, or unzip archive.zip -d /target to extract into a specific folder.

How do I password-protect a zip file?+

Use zip -e -r secure.zip folder, which prompts for a password. Note that ZIP's built-in encryption is weak; use GPG for sensitive data.

How do I list the contents of a zip without extracting?+

Use unzip -l archive.zip to see the file list, sizes, and dates before you extract anything.

Should I use zip or tar.gz?+

Use tar.gz for Linux-to-Linux transfers and backups, where it preserves permissions and compresses well. Use zip when sharing with Windows or macOS users, who can open it without extra tools.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support