Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

zip, unzip

Pack files into a .zip archive (zip) and unpack it (unzip) — the format that Windows and macOS open directly.

Level
Everyday Linux
Group
Archive and compress
Needs root
No
Package
zip, unzip
Also called
unzip

What it does

zip makes one archive with many files and folders, compressed. unzip extracts it. Use zip when you exchange files with Windows users or upload to tools that want .zip.

For Linux backups, tar is better: zip does not keep Linux owners and some permissions the same way.

How to write it

zip [OPTIONS] ARCHIVE.zip FILE...
unzip [OPTIONS] ARCHIVE.zip [FILE...] [-x EXCLUDE...] [-d DIR]

Options 50

Most used

zip

-r
Include folders and everything in them (recursive).
-q
Quiet: no messages.
-v
Verbose output.
-0 ... -9
Compression level: 0 = store only, 9 = best. Default 6.
-e
Encrypt the files; zip asks for a password.
-P password
Give the password on the command line (unsafe: it is in the history).
-x patterns
Leave out files that match.
-i patterns
Include only files that match.
-u
Update: add new files and replace changed files in the archive.
-f
Freshen: replace changed files only (do not add new ones).
-m
Move: delete the original files after zipping.
-d
Delete files from the archive.
-j
Junk paths: store only file names, no folders.
-y
Store symbolic links as links (not the files they point to).
-T
Test the new archive after writing it.
-s size
Split the archive into parts of this size (like 100m).
-l, -ll
Convert line endings (LF to CRLF / CRLF to LF) for text files.
-o
Set the archive time to the newest file's time.
-z
Add a comment to the archive.
-c
Add a one-line comment for each file.
-@
Read the file names from standard input.
-D
Do not add separate entries for folders.
-n suffixes
Do not compress files with these endings (like .jpg:.png).
-b path
Folder for the temporary file.
-X
Do not save extra file attributes (Unix UID/GID, times).
-F, -FF
Try to fix a damaged archive.
-A
Fix the offsets of a self-extracting archive.
-g
Grow (add to) an existing archive.
-t date, -tt date
Only files changed on or after / before this date.
-S
Include system and hidden files (on MS-DOS/Windows).
-h, -h2
Show help (-h2 = extended help).

unzip

-l
List the files in the archive (short).
-v
List with details (verbose).
-t
Test the archive.
-d dir
Extract into this folder.
-o
Overwrite existing files without asking.
-n
Never overwrite existing files.
-q
Quiet.
-j
Junk paths: extract all files into one folder.
-x files
Do not extract these files.
-p
Extract to stdout (pipe) without messages.
-c
Extract to the screen with the file names.
-f
Freshen: extract only files that already exist and are newer in the archive.
-u
Update: extract new files and newer versions of existing files.
-P password
The password for encrypted archives (unsafe on the command line).
-a
Convert text files to the local line endings.
-L
Make names lowercase.
-C
Match names without upper/lower case.
-z
Show only the archive comment.
-Z
Work like zipinfo (detailed listing).

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Zip a folder

    zip -r report.zip report/

    -r takes the whole folder.

  2. 2Unzip into a folder

    unzip report.zip -d /tmp/report

    The folder is made if it does not exist.

  3. 3List the content before you extract

    unzip -l upload.zip

    Check for strange paths like ../ before you trust a zip from outside.

  4. 4Zip with a password

    zip -e -r secret.zip contracts/

    zip asks for the password. Note: classic zip encryption is weak; use 7z or gpg for real secrets.

  5. 5Leave out files

    zip -r site.zip public_html -x '*.log' '*/.git/*'

    Smaller archive, no logs, no git data.

  6. 6Add or update files in an archive

    zip -u site.zip public_html/index.html

    Only changed files are replaced.

  7. 7Extract only one file

    unzip site.zip public_html/index.html -d /tmp

    Give the path as it is stored in the archive.

  8. 8Test an archive

    unzip -t backup.zip

    "No errors detected" means the file is complete.

  9. 9Overwrite without questions in a script

    unzip -o -q release.zip -d /srv/app

    -o overwrites, -q keeps the log clean.

  10. 10Store files without folders

    zip -j logs.zip /var/log/httpd/*.log

    All files at the top level of the zip.

  11. 11Split a big archive for upload limits

    zip -r -s 100m dataset.zip dataset/

    Makes dataset.z01, dataset.z02 … and dataset.zip.

  12. 12Make a zip from a list of files

    find . -name "*.pdf" | zip invoices.zip -@

    The names come from find.

Watch out

  • Check the content of an unknown zip with unzip -l before you extract (path tricks, very large "zip bombs").
  • -P password is saved in the shell history; use -e so it is asked.
  • zip does not keep Linux owners on extract; use tar for system backups.

Tips to remember

  • Pack: zip -r name.zip folder. Unpack: unzip name.zip -d folder. Look: unzip -l.

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man zip