The zip Command Explained
zip is the archive format most people outside the Linux world expect by default. Where tar paired with gzip or xz is the native Linux convention, zip earns its place specifically because Windows and macOS both open it natively without any additional software, making it the pragmatic choice whenever an archive needs to leave a Linux-only environment.
Basic Usage
zip archive.zip file1.txt file2.txt
Creates archive.zip containing the listed files. Unlike gzip or bzip2, the original files are left untouched; zip always produces a separate archive file rather than replacing the source.
Archiving a Directory
zip -r archive.zip folder/
The -r (recursive) flag is required to include a directory’s contents. Without it, zip will only add files explicitly named on the command line and won’t descend into subdirectories.
Compression Levels
zip -9 -r archive.zip folder/ # maximum compression
zip -0 -r archive.zip folder/ # store only, no compression
Levels range from -0 (store only, useful for bundling already-compressed content like JPEGs without wasting time trying to compress it further) to -9 (maximum compression), with -6 as the default.
Password Protection
zip -e archive.zip file1.txt file2.txt
The -e flag prompts interactively for a password without echoing it to the terminal or shell history. Avoid the --password yourpassword form, since it leaves the password visible in shell history and in the process list while the command runs.
It’s worth knowing that the traditional zip encryption scheme is considered weak by modern cryptographic standards. It will keep out casual snooping but shouldn’t be relied on to protect genuinely sensitive data; for that, encrypt with a stronger, purpose-built tool (like GPG) before or instead of relying on zip’s built-in encryption.
Adding Files to an Existing Archive
zip archive.zip newfile.txt
Running zip again against an existing archive adds new files or updates changed ones, without needing to recreate the whole archive from scratch.
Removing Files from an Archive
zip -d archive.zip file-to-remove.txt
-d deletes a specific file from an existing archive without needing to rebuild it.
zip vs. tar+gzip/xz
| Aspect | zip | tar + gzip/xz |
|---|---|---|
| Compression approach | Per-file, individually | Whole archive as one stream |
| Overall compression ratio | Typically worse | Typically better |
| Cross-platform support | Native on Windows/macOS/Linux | Requires extra tools on Windows |
| Unix permissions/ownership | Limited, inconsistent | Fully preserved |
| Extracting a single file | Fast, no need to read whole archive | Slower on compressed archives |
The per-file compression approach in zip means the ratio across a whole archive is usually worse than tar piped through a single compressor, since the compressor can’t find patterns that span multiple files when each one is compressed independently. The tradeoff is that extracting or replacing a single file in a zip archive is fast and doesn’t require processing the rest of the archive, which isn’t true for a single continuous compressed stream like .tar.gz.
When to Choose zip on Linux
- Sharing with non-Linux users: recipients on Windows or macOS can open
.zipwithout installing anything extra - Cross-platform tooling and CI pipelines: many build and deployment tools expect
.zipspecifically (Lambda deployment packages, Java.jar/.warfiles, and many plugin/extension formats are zip-based under the hood) - Needing to update or extract individual files from an archive without touching the rest
For Linux-to-Linux transfers, backups, and anything where preserving Unix permissions and ownership matters, tar (paired with gzip, bzip2, or xz) remains the better default.
Frequently Asked Questions
How is zip different from tar combined with gzip or xz?
zip combines archiving and compression into a single format and a single file-by-file process, where each file inside the archive is compressed individually. tar plus a compressor archives everything into one continuous stream first, then compresses that whole stream as a unit, which usually achieves better overall compression since patterns across files can be exploited, but means the entire archive typically needs to be read to extract even a single file efficiently. zip individual per-file compression makes it easier to extract or replace a single file without touching the rest of the archive.
Does zip preserve Linux file permissions and ownership?
zip has limited and inconsistent support for Unix permissions compared to tar, which was designed around Unix filesystem semantics from the start. Basic executable bits are often preserved, but zip is not a reliable choice for backing up or transferring files where exact ownership, full permission bits, and special file types need to be preserved precisely. For Linux-to-Linux transfers where permissions matter, tar is the better choice; zip is best suited for cross-platform sharing where those details are less important.
How do I create a zip archive of an entire directory?
Use zip -r archive.zip directory/, where the -r flag tells zip to recurse into subdirectories. Without -r, zip only adds files directly named on the command line and will not descend into a directory automatically.
How do I password-protect a zip archive?
Use zip -e archive.zip files or zip —password yourpassword archive.zip files to encrypt the archive contents with a password. The —password flag exposes the password in shell history and process listings, so the interactive -e prompt (which asks for the password without displaying or logging it) is the safer choice. Note that the traditional zip encryption method is considered weak by modern standards and should not be relied on for protecting genuinely sensitive data.
What compression level options does zip support?
zip supports compression levels from -0 (store only, no compression) to -9 (maximum compression), with -6 as the default, matching the same general convention used by gzip. -0 is useful when you want a zip file purely as a container without spending time compressing already-compressed content, such as bundling files that are already JPEG images or other pre-compressed formats.
Why would a Linux user choose zip over tar.gz when both are available?
The main reason is compatibility with recipients who are not on Linux. Windows and macOS have built-in support for opening zip files without any extra software, while tar.gz files often require additional tools on Windows in particular. When sharing an archive with someone whose operating system is unknown or likely to be Windows or macOS, zip is the safer default despite tar-based formats generally compressing better and preserving Unix metadata more faithfully.