ZIP is the archive format everyone can open — Windows, macOS, Linux, phones. On Linux servers tar.gz is more common, but when you exchange files with Windows users or upload a theme or plugin, you’ll need zip and unzip.
Install
sudo apt install zip unzip # Debian/Ubuntu
sudo dnf install zip unzip # RHEL family
Create a zip archive
zip archive.zip file1.txt file2.txt
zip -r site.zip /var/www/example.com # recursive: folders and contents
zip -r site.zip public_html # relative path inside the archive
-r is required for directories — without it, zip adds only the empty folder entry.
Paths inside the archive mirror what you pass in. To avoid long absolute paths, cd first:
cd /var/www && zip -r ~/example.zip example.com
Compression level and speed
zip -r -9 archive.zip folder # maximum compression (slower)
zip -r -1 archive.zip folder # fastest
zip -r -0 archive.zip folder # store only (good for already-compressed media)
Exclude files
zip -r site.zip example.com -x "*.log" "*/node_modules/*" "*/.git/*"
zip -r site.zip example.com [email protected]
Quote patterns so the shell doesn’t expand them.
Update and add to an archive
zip archive.zip newfile.txt # add or replace
zip -u archive.zip folder/* # update changed files only
zip -d archive.zip "*.tmp" # delete entries from the archive
Password protection
zip -e -r secret.zip folder # prompts for a password
Classic ZIP encryption (“ZipCrypto”) is weak and can be cracked. For real protection use 7-Zip with AES-256:
sudo apt install 7zip # or p7zip-full on older releases
7z a -p -mhe=on secret.7z folder # AES-256, encrypts file names too
Or encrypt with GPG: gpg -c archive.tar.gz. See GPG encryption.
Extract
unzip archive.zip # into the current directory
unzip archive.zip -d /tmp/extracted # into a directory (created if needed)
unzip -o archive.zip # overwrite without asking
unzip -n archive.zip # never overwrite existing files
unzip archive.zip "docs/*" # only some files
unzip -j archive.zip "*.pdf" -d pdfs/ # flatten: ignore folder structure
List and test
unzip -l archive.zip # list contents
unzip -v archive.zip # verbose: sizes, compression ratio, CRC
unzip -t archive.zip # test integrity
zipinfo archive.zip
Always list an archive from an unknown source before extracting: a malicious zip may contain paths like ../../etc/cron.d/x (unzip blocks .. paths by default, but caution is free) or thousands of files.
View a file without extracting
unzip -p archive.zip config/settings.json | less
Large files and many files
The original ZIP format is limited to 4 GB per file and 65,535 entries; modern zip/unzip builds support Zip64 extensions that lift those limits. For very large server backups, tar with zstd compression is faster and preserves Unix permissions and ownership better — see tar examples.
Split a huge archive into parts (e.g. for upload limits):
zip -r -s 1g backup.zip folder # parts of 1 GB: backup.z01, backup.z02, …, backup.zip
zip -s 0 backup.zip --out single.zip # rejoin
Permissions and Unix details
ZIP stores basic Unix permissions when created with zip on Linux, but not ownership, ACLs or extended attributes. After unzipping web files, reset permissions:
find site -type d -exec chmod 755 {} +
find site -type f -exec chmod 644 {} +
See chmod.
Character encoding
Archives created on Windows with non-ASCII file names (Chinese, Japanese, Cyrillic) sometimes extract with garbled names. Newer unzip versions on some distributions support -O to specify the encoding: unzip -O GBK archive.zip. Alternatively use 7z x, which handles most encodings well.
Quick reference
| Task | Command |
|---|---|
| Zip a folder | zip -r out.zip folder |
| Exclude | zip -r out.zip folder -x "*.log" |
| Encrypt (weak) | zip -e -r out.zip folder |
| Extract here | unzip file.zip |
| Extract to dir | unzip file.zip -d dir |
| List | unzip -l file.zip |
| Test | unzip -t file.zip |
| AES encryption | 7z a -p -mhe=on out.7z folder |