Linux Command Reference ยท Archives and Compression
Linux zip and unzip commands
Reference for the Linux zip and unzip commands: create ZIP archives, add and delete members, extract to a directory with -d, list with -l, test with -t and exclude files.
The ZIP format combines archiving and compression in one file: each member is compressed on its own and a central directory at the end lists them all. That makes it easy to extract a single file quickly, and it is the format that Windows, macOS, Java (.jar), Android (.apk) and Office documents use. On Linux the Info-ZIP tools zip and unzip handle it.
Choose ZIP when you exchange files with people on other systems. For Linux-only backups, tar keeps permissions, owners and links more faithfully.
zip -r site.zip site/ unzip site.zip -d /tmp/site
Try it
The learning sandbox has unzip but not zip, so creating archives is read-only here. The runnable examples build a small ZIP file by hand with a here-document (a base64 encoded archive) and then use unzip to inspect it.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
zip [OPTIONS] ARCHIVE.zip FILE... unzip [OPTIONS] ARCHIVE.zip [MEMBER...] [-x PATTERN...] [-d DIR]
| Command / option | Description |
|---|---|
zip -r a.zip dir |
Create or add to the archive, descending into directories. |
zip -9 / -0 |
Best compression / store without compressing. |
zip -e |
Encrypt with a password (the legacy ZipCrypto method is weak). |
zip -d a.zip NAME |
Delete a member. |
zip -u a.zip FILE |
Update only changed files. |
zip -x 'pattern' |
Exclude names (-x '*.git*'). |
zip -q, -j |
Quiet; "junk" the directory paths and store only file names. |
unzip -l / -v |
List contents / list verbosely with methods and CRC. |
unzip -t |
Test integrity. |
unzip -d DIR |
Extract into DIR. |
unzip -p |
Extract to standard output (pipe friendly). |
unzip -o / -n |
Overwrite without prompting / never overwrite. |
unzip -j |
Ignore stored directories; put all files in one place. |
unzip -Z |
zipinfo mode; -Z1 prints names only. |
Returns: zip: 0 for success, 12 when there was nothing to do, 15 for a file that could not be opened. unzip: 0 for success, 1 for warnings, 2 and above for errors (3 means a severe problem in the archive, 9 means the file was not found).
Usage notes
- Without
-r,zipstores only the directory entry, not what is inside it. Alwayszip -r archive.zip folder. unzipasks before overwriting. In scripts use-oor-nso it never waits for input.- Trust the paths less than the file. Archives can contain
../; recentunzipblocks the worst cases, but still list with-land extract into an empty directory with-d. - Linux metadata survives only partly. Basic permissions are stored, but ownership, ACLs and extended attributes are not. Symbolic links need
zip -y. - Compression is per member, so ZIP gains less than
tar.gzon many small similar files, but allows random access. - Large files. Info-ZIP supports Zip64 (more than 4 GiB or 65,535 members) in current versions.
- Passwords.
zip -euses a legacy cipher that is easy to break; for real secrecy encrypt withgpgorage, or use 7-Zip's AES mode. - File names and encodings. Names are UTF-8 on modern tools, but archives from older Windows systems may use a local code page; use
unzip -O cp437or similar when names look wrong. - Alternatives.
bsdtar -xf file.zipand7z xalso read ZIP;jarandpython -m zipfilework in environments with no unzip.