zip — Create and Extract ZIP Archives
Practical guide to zip and unzip: create, extract, compress and encrypt ZIP archives – the most universal cross-platform archive format.
zip and unzip are the classic tools for the ZIP format – the one archive format you can open virtually anywhere, from Windows to macOS to Linux. With zip you bundle and compress files in a single step; with unzip you get them back out. This guide walks you through the commands you reach for daily: recursive archiving, compression levels, exclude patterns, encryption and extracting individual files on demand.
Create Archives
zip <archive>.zip <files> — Create a ZIP archive from files.
zip backup.zip file1.txt file2.txtzip -r <archive>.zip <dir> — Create a ZIP archive from a directory (recursive).
zip -r project.zip project/zip -j <archive>.zip <files> — Create archive without directory paths (junk paths).
zip -j flat.zip path/to/file1.txt path/to/file2.txtzip -<level> <archive>.zip <files> — Set compression level (0=store, 1=fastest, 9=best).
zip -9 best.zip largefile.datzip -0 <archive>.zip <files> — Store files without compression (faster for pre-compressed data).
zip -0 images.zip *.jpgAdd, Update & Delete
zip -u <archive>.zip <files> — Update: add new or changed files to an existing archive.
zip -u backup.zip newfile.txtzip -d <archive>.zip <files> — Delete files from an existing archive.
zip -d backup.zip oldfile.txtzip -g <archive>.zip <files> — Grow: append files to an existing archive.
zip -g backup.zip extra.txtExclude & Filter
zip -r <archive>.zip <dir> -x '<pattern>' — Create archive excluding files matching a pattern.
zip -r project.zip project/ -x '*.git*' '*node_modules*'zip -r <archive>.zip <dir> -x '*.log' '*.tmp' — Exclude multiple file patterns.
zip -r deploy.zip src/ -x '*.log' '*.tmp' '*.test.js'zip -r <archive>.zip <dir> -i '*.php' '*.html' — Include only files matching patterns.
zip -r code.zip project/ -i '*.php' '*.html' '*.css'Encryption & Split
zip -e <archive>.zip <files> — Create a password-encrypted ZIP archive.
zip -e -r secret.zip confidential/zip -P '<password>' <archive>.zip <files> — Create encrypted archive with inline password (insecure in history).
zip -P 'mypass' secret.zip file.txtzip -s <size> -r <archive>.zip <dir> — Create a split archive (multi-part).
zip -s 100m -r large.zip bigfolder/unzip — Extract
unzip <archive>.zip — Extract all files from a ZIP archive.
unzip backup.zipunzip <archive>.zip -d <dir> — Extract to a specific directory.
unzip backup.zip -d /tmp/restore/unzip <archive>.zip '<file>' — Extract only specific files.
unzip backup.zip 'config.yaml'unzip -o <archive>.zip — Overwrite existing files without prompting.
unzip -o update.zip -d /var/www/unzip -n <archive>.zip — Never overwrite existing files.
unzip -n backup.zipunzip -l <archive>.zip — List contents of a ZIP archive without extracting.
unzip -l backup.zipunzip -t <archive>.zip — Test archive integrity without extracting.
unzip -t backup.zipunzip -p <archive>.zip '<file>' — Extract a file to stdout (pipe to another command).
unzip -p backup.zip config.json | jq . Conclusion
The ZIP format wins on ubiquity above all: if you need to hand an archive to someone on another platform, zip is almost always a safe bet. Don't rely on its built-in encryption, though – the legacy ZipCrypto scheme (zip -e, zip -P) is considered weak and easy to break, and -P writes your password straight into the shell history. For genuine confidentiality, reach for AES-capable tools such as 7-Zip, or age and GPG.
Further Reading
- Info-ZIP – official project site – home of zip and unzip, with documentation and source code
- zip(1) – Linux man page – complete option reference for the zip command