The zip Commands You Will Use Most
zip notes.zip notes.txt todo.txt
zip -r site.zip site
zip -r site.zip site -x 'site/cache/*'
unzip -l site.zip
The archive name comes first, then the files to add. -r adds a folder with everything inside it, -x leaves out paths that match a pattern, and unzip -l lists the result. If the archive already exists, zip adds the files to it instead of starting a new one.
Install zip
sudo apt install zip
sudo dnf install zip
The first line is for Ubuntu and Debian, the second for AlmaLinux and Rocky Linux. zip and unzip are separate packages, and minimal server images often include neither. If the shell reports that zip is not found, installing the package is the fix; command not found covers the general case.
Zip a Folder
cd /var/www
zip -r ~/example-site.zip example.com
Without -r, zip adds only the folder entry and none of its files. zip stores paths as you type them, so run it from the parent folder: the archive then holds example.com/ and not var/www/example.com/. Hidden files such as .htaccess are included when you name the folder itself, but not when you write example.com/*; see how to show hidden files. zip follows symbolic links and stores the files they point to; add -y to store the links themselves.
Leave Files Out
zip -r site.zip site -x 'site/node_modules/*' '*.log'
Patterns after -x are compared with the paths as they are stored in the archive, and * also matches across slashes. Quote each pattern so the shell does not expand it first. Check the result with unzip -l site.zip.
Add, Update, and Remove Entries
| Command | What it does |
|---|---|
zip site.zip new.html | Adds or replaces a file |
zip -ru site.zip site | Adds only new or changed files |
zip -d site.zip 'site/old.html' | Removes an entry |
zip -T site.zip | Tests the archive |
-m moves files into the archive and deletes the originals afterward. Avoid it unless you have another copy.
Compression Levels
zip -9 -r logs.zip logs
zip -0 -r photos.zip photos
Levels run from -0 (store only, no compression) to -9 (smallest and slowest); the default is -6. Text, SQL dumps, and logs shrink a lot. Images, videos, and files that are already compressed barely shrink, so -0 saves time for them.
Password Protection Is Weak
zip -e -r private.zip private
-e asks for a password twice and encrypts the files with the classic ZIP method. That method is old and weak: known attacks can recover the contents, so treat it as protection against casual opening only. Never pass the password with -P on the command line, because it ends up in your shell history and is visible in the process list. For data that must stay private, encrypt the archive with gpg: gpg -c private.zip asks for a passphrase and writes private.zip.gpg. To move it to another server, use scp, which encrypts the transfer.
zip vs tar.gz
| Point | zip | tar.gz |
|---|---|---|
| Owners and groups | Not restored by default | Restored as root |
| Symbolic links | Followed by default | Kept as links |
| One file out | Fast | Reads the archive |
| Other computers | Opens almost anywhere | May need a tool |
For server backups and moving sites between Linux servers, tar.gz is the better fit because it keeps permissions, owners, and links; see how to extract tar.gz files. zip suits files you send to people who open them on their own computers. To extract a zip archive on the server, see how to unzip files in Ubuntu.