zip: Archive and compress files


Quick start

Create a simple ZIP archive containing files:

zip myarchive.zip file1.txt file2.txt

Add a whole directory recursively:

zip -r myarchive.zip path/to/directory

Inspect the contents of a ZIP without extracting:

zip -sf myarchive.zip

Common workflows

  • Add files to an existing archive:
zip existing.zip newfile.txt anotherdir/
  • Exclude certain files while zipping a directory:
zip -r myarchive.zip path/to/dir -x 'path/to/dir/exclude/**'
  • Create an encrypted archive (password required to extract):
zip -e encrypted.zip secret.txt data.csv
  • Use a specific compression level (0 is store, 9 is best compression):
zip -r9 best.zip folder/
  • Add multiple files with patterns (example):
zip archive.zip *.txt logs/*.log
  • Create a split archive (useful for large backups):
zip -s 3g -r backup.zip /path/to/backup
  • Update a ZIP with newer versions of files (zip will replace only newer files):
zip -u archive.zip file1.txt

Common pitfalls

  • Forgetting -r when archiving directories can result in only top-level files being added.
  • Paths matter: run from the directory that makes the paths inside the archive predictable, or use absolute paths carefully.
  • Excluding with -x uses patterns; ensure your wildcard patterns match what you intend.
  • Encryption options vary across versions; some builds support -e for password prompts, others offer -P for a specific password (less secure in shell history).
  • Large archives: splitting helps with media constraints, but ensure the splitting option matches your drive/media needs.

TL;DR quick references

  • Add files:
zip archive.zip file1.txt file2.txt
  • Recursively add a directory:
zip -r archive.zip path/to/dir
  • Exclude items:
zip -r archive.zip path/to/dir -x 'path/to/dir/exclude/**'
  • Encrypt:
zip -e archive.zip sensitive.txt
  • Split a archive:
zip -s 3g -r archive.zip large_dir/
  • Show contents:
zip -sf archive.zip

See Also