zip Command

The zip command creates ZIP archive files in Linux and Unix systems. ZIP files are compressed archives that are widely compatible across different operating systems, making them ideal for file distribution and storage.

Syntax

zip [options] archive.zip files...

Description

The zip command creates compressed archive files in the ZIP format. Unlike tar, ZIP files include compression by default and are natively supported by most operating systems including Windows, macOS, and Linux.

Key features:

  • Built-in compression (deflate algorithm)
  • Cross-platform compatibility
  • Individual file compression within archive
  • Password protection support
  • Preserves file metadata and permissions

🌐 Cross-Platform Compatibility

ZIP files created on Linux can be opened on:

  • Windows: Built-in support, WinRAR, 7-Zip
  • macOS: Built-in Archive Utility
  • Linux: unzip command, file managers
  • Mobile: iOS Files app, Android file managers

Common Options

Option Description Example
-r Recursively archive directories zip -r archive.zip folder/
-v Verbose output zip -v archive.zip files
-0 to -9 Compression level (0=none, 9=max) zip -9 archive.zip files
-e Encrypt with password zip -e archive.zip files
-x Exclude files zip -r archive.zip folder/ -x "*.tmp"
-u Update existing archive zip -u archive.zip newfile.txt
-d Delete files from archive zip -d archive.zip oldfile.txt

Compression Levels

  • -0: No compression (store only) - fastest
  • -1: Minimal compression - fast
  • -6: Default compression - balanced
  • -9: Maximum compression - slowest

Trade-off: Higher compression = smaller files but slower processing

ZIP vs TAR Comparison

ZIP Advantages
  • Cross-platform compatibility
  • Built-in compression
  • Individual file access
  • Password protection
TAR Advantages
  • Better for Unix/Linux systems
  • Preserves permissions better
  • More compression options
  • Streaming support

Examples

Create basic ZIP archives

zip archive.zip file1.txt file2.txt # Archive specific files
zip documents.zip *.pdf # Archive all PDF files
zip -r project.zip project_folder/ # Archive entire directory

Create ZIP archives with files and directories

Compression levels

zip -0 fast.zip files/ # No compression (fastest)
zip -6 balanced.zip files/ # Default compression
zip -9 small.zip files/ # Maximum compression (smallest)

Control compression level for speed vs size trade-offs

Password protection

zip -e secure.zip sensitive_files/ # Prompts for password
zip -P mypassword protected.zip files/ # Password on command line (less secure)

Create password-protected ZIP archives for security

Exclude files and patterns

zip -r backup.zip project/ -x "*.tmp" "*.log"
zip -r source.zip src/ -x "*/node_modules/*" "*/.git/*"
zip -r clean.zip folder/ -x "*.o" "*.exe" "*~"

Create archives while excluding unwanted files and directories

Update and modify archives

zip -u archive.zip newfile.txt # Add or update file
zip -d archive.zip oldfile.txt # Delete file from archive
zip -f archive.zip # Freshen (update changed files only)

Modify existing ZIP archives without recreating them

Extract ZIP files (unzip)

unzip archive.zip # Extract to current directory
unzip archive.zip -d /tmp/extracted/ # Extract to specific directory
unzip -l archive.zip # List contents without extracting
unzip -t archive.zip # Test archive integrity

Extract and examine ZIP archives using the unzip command

💡 Tips and Best Practices

  • Use -r for directories: Always include -r when archiving folders
  • Test archives: Use unzip -t to verify archive integrity
  • Choose compression wisely: Use -9 for distribution, -0 for temporary files
  • Exclude unnecessary files: Skip temp files, logs, and build artifacts
  • Password security: Use -e (prompt) instead of -P for passwords
  • Check compatibility: ZIP works everywhere, but consider tar for Linux-only use

Common Use Cases

  • Software distribution: zip -r myapp-v1.0.zip myapp/ -x "*.git/*"
  • Document backup: zip -9 documents-$(date +%Y%m%d).zip ~/Documents/
  • Web deployment: zip -r website.zip public/ -x "*.tmp" "*.log"
  • Source code archive: zip -r source.zip src/ -x "*/build/*" "*/node_modules/*"
  • Secure file transfer: zip -e confidential.zip sensitive_data/

Troubleshooting

  • "zip command not found": Install with sudo apt install zip (Ubuntu/Debian)
  • Large files: ZIP has 4GB file size limit (use ZIP64 for larger files)
  • Permission errors: Ensure read access to source files
  • Path issues: Use relative paths to avoid absolute path problems
  • Corruption: Test archives with unzip -t before distribution

See also