zip & unzip
The .zip format is the archive format of Windows and macOS users, email attachments and many downloads (Java .jar files and Office .docx files are zip archives too). Unlike tar, a zip file compresses each file separately and is understood natively by every desktop OS, which makes it the friendliest choice for sharing with non-Linux users.
Creating a zip
zip ARCHIVE FILES... adds files to an archive (creating it if needed). Use -r to include directories:
mkdir -p report/charts
for i in $(seq 1 50); do echo "row $i,region north,sales 100"; done > report/data.csv
echo "Q1 summary" > report/summary.txt
echo "PNG" > report/charts/sales.png
zip -r report.zip report/data.csv report/summary.txt report/charts adding: report/data.csv (deflated 90%)
adding: report/summary.txt (stored 0%)
adding: report/charts/ (stored 0%)
adding: report/charts/sales.png (stored 0%)
Here the files are named one by one so they are added in a fixed order; -r makes zip descend into report/charts. Often you'd simply write zip -r report.zip report. zip prints each file it adds and how much it deflated (compressed) it. The repetitive CSV shrinks a lot; tiny files are just stored.
Note: Without
-r,zip report.zip reportwould add only the empty directory entry, not its contents. That's a common surprise.
Listing a zip
unzip -l lists the contents with sizes and dates (here the files' times are fixed with touch so the listing is reproducible):
mkdir -p photos
echo "jpeg data" > photos/beach.jpg
echo "jpeg data 2" > photos/city.jpg
touch -d '2026-07-04 18:30' photos/*.jpg photos
zip -q photos.zip photos photos/beach.jpg photos/city.jpg
unzip -l photos.zipArchive: photos.zip
Length Date Time Name
--------- ---------- ----- ----
0 2026-07-04 18:30 photos/
10 2026-07-04 18:30 photos/beach.jpg
12 2026-07-04 18:30 photos/city.jpg
--------- -------
22 3 files
-q (quiet) suppresses the "adding:" lines.
Extracting
mkdir -p docs && echo "# Manual" > docs/manual.md && echo "notes" > docs/notes.txt
zip -q docs.zip docs docs/manual.md docs/notes.txt
rm -r docs
unzip docs.zip
echo "---"
unzip -q docs.zip -d copy
find copy | sort
echo "---"
unzip -p docs.zip docs/manual.mdArchive: docs.zip
creating: docs/
extracting: docs/manual.md
extracting: docs/notes.txt
---
copy
copy/docs
copy/docs/manual.md
copy/docs/notes.txt
---
# Manual
| Option | Effect |
|---|---|
-d DIR |
Extract into DIR |
-q |
Quiet |
-o |
Overwrite existing files without asking |
-n |
Never overwrite existing files |
-p |
Print a file's content to the screen |
-l |
List contents |
If a file already exists, unzip asks interactively whether to replace it. In scripts, decide in advance with -o or -n so it never stops to ask.
Updating, excluding and deleting
mkdir -p proj && echo "a" > proj/main.py && echo "tmp" > proj/cache.tmp
zip -qr proj.zip proj -x '*.tmp'
echo "b" > proj/util.py
zip -qr proj.zip proj -x '*.tmp'
zipinfo -1 proj.zip
zip -d proj.zip proj/util.py
zipinfo -1 proj.zipproj/
proj/main.py
proj/util.py
deleting: proj/util.py
proj/
proj/main.py
Running zip again on an existing archive adds new files and updates changed ones. -x PATTERN excludes files; zip -d deletes entries. zipinfo -1 lists just the names, one per line, which is easy to process in scripts.
Password protection
zip -e secret.zip file asks for a password and encrypts the archive. The classic zip encryption is weak, so for anything truly sensitive use gpg or 7z with AES instead.
tar.gz or zip?
tar.gz |
zip |
|
|---|---|---|
| Native on | Linux, macOS | Windows, macOS, Linux |
| Keeps Unix permissions/owners | Yes | Partly |
| Compression | Whole stream (better ratio for many small files) | Per file |
| Extract one file quickly | Must read through the stream | Yes, random access |
| Typical use | Backups, source releases, servers | Sharing with anyone |
Test yourself
Which command zips the directory site including its contents?
- zip site.zip site
- zip -r site.zip site
- unzip -r site site.zip
- zip -l site.zip site
How do you extract pack.zip into the folder out?
- unzip pack.zip > out
- unzip -d out pack.zip
- unzip pack.zip -C out
- zip -x pack.zip out
Which command lists the files in pack.zip without extracting?
- unzip -l pack.zip
- zip -l pack.zip
- unzip -p pack.zip
- cat pack.zip