Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

zip Command Guide for Cross-Platform Archives

zip creates the .zip archive format that every desktop operating system can open natively — the format you reach for whenever a file has to travel to Windows or macOS.


What is zip, and Why Use It?

zip bundles files into a single .zip container with built-in compression, and unzip opens them back up. Unlike tar, which is Linux-native, .zip is understood by Windows Explorer, macOS Finder, and file managers everywhere — it is the lingua franca of “here is a folder, open it”. The trade-off: .zip does not preserve Linux file permissions and ownership the way tar does, so it is best for sharing documents and media, not for system backups.

Why Learn zip?

  • Universally readable — no extra software needed on the receiving machine.
  • The format you will get asked for — “send me a zip” is a common request.
  • Password protection and splitting — built-in options tar lacks.
  • unzip -l lists contents — inspect before extracting, same as tar -t.

zip is about sharing, not backing up. If the archive needs to preserve permissions or live on a Linux server, use tar; if a human on another OS needs to open it, use zip.


1. Create a Zip Archive

To compress a single file:

zip documents.zip report.pdf

To bundle a whole directory, add -r (recursive):

zip -r project.zip myproject/

-r walks the directory tree and stores every file, keeping the folder structure inside the archive. Without -r, zip complains that myproject/ is a directory.

Check the result:

ls -lh project.zip

2. Add Files to an Existing Archive

You can keep appending to a zip:

zip project.zip notes.md
zip -u project.zip src/main.c
  • -u — update: only replace files that are newer than what is already in the archive.

3. Extract an Archive

unzip project.zip

This unpacks into the current directory. To choose a destination:

unzip project.zip -d ~/restored/
  • -d — the destination directory (created if missing).

Unzipping into a fresh directory is the safe habit — running unzip in a directory full of same-named files invites overwrites.

4. List Contents Without Extracting

unzip -l project.zip
  • -l — list the table of contents.

Output shows sizes, dates, and paths:

Archive:  project.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2026-12-05 10:00   myproject/
     1024  2026-12-05 10:00   myproject/README.md
---------                     -------
     1024                     2 files

To extract just one file from the archive:

unzip project.zip myproject/README.md

5. Control What Gets Included

Exclude paths with -x:

zip -r project.zip myproject/ -x "*.git*" -x "node_modules/*"

Compress more aggressively with a level flag:

zip -r -9 project.zip myproject/
  • -1 through -9 — compression level; -9 is smallest, -1 is fastest. The default is -6.

6. Password-Protect an Archive

zip -er secret.zip sensitive/
  • -e — encrypt (prompt for a password).

You will be asked twice. Extraction requires the same prompt:

unzip secret.zip

7. Test and Repair

Check a suspect download:

unzip -t project.zip
  • -t — test the integrity of the archive.

To overwrite existing files during extraction without being asked:

unzip -o project.zip
  • -o — overwrite without prompting.

Common Pitfalls

  • Forgetting -r on directories — zip archive.zip myfolder/ alone refuses to work; add -r to include the tree.
  • Expecting permission preservation — zip does not reliably store Linux permissions and ownership; use tar for backups.
  • Extracting over existing files — plain unzip asks before overwriting; -o skips the questions but can clobber work. Extract into a fresh directory when unsure.
  • Assuming zip works everywhere the same — some archives created on Windows with odd filenames extract with mangled names on Linux; prefer ASCII filenames when sharing.
  • Using zip for huge files — .zip has a 4 GB per-file limit in the classic format; for very large data use tar with xz or split archives.
PageRelationship
tarThe Linux-native archive format; preserves permissions
gzipThe single-file compressor inside .tar.gz and .zip
xzHigher-ratio compression for .tar.xz
bzip2Older compressor used by some .zip implementations

zip Cheatsheet

CommandWhat it does
zip file.zip doc.pdfZip a single file
zip -r archive.zip dir/Recursively zip a directory
zip -u archive.zip fileUpdate only newer files in the archive
unzip archive.zipExtract into the current directory
unzip archive.zip -d /path/Extract into a specific directory
unzip -l archive.zipList contents without extracting
unzip archive.zip path/insideExtract one file
zip -r -9 archive.zip dir/Zip with maximum compression
zip -er secret.zip dir/Create a password-protected archive
unzip -t archive.zipTest archive integrity
unzip -o archive.zipExtract, overwriting without prompting