Bash Compress (zip)

Learn to compress files and folders using zip command

📦 What is Zip Compression?

Zip is a popular compression utility that reduces file sizes and bundles multiple files into a single archive. It's widely supported across all operating systems and perfect for sharing files efficiently.


# Simple zip example
zip myarchive.zip file.txt
                                    

Output:

adding: file.txt (deflated 45%)

Key Zip Concepts

📄

Single File

Compress one file quickly

zip archive.zip document.txt
📁

Multiple Files

Compress several files together

zip archive.zip file1.txt file2.txt
🗂️

Folders

Compress entire directories

zip -r archive.zip folder/
🔒

Password

Protect with encryption

zip -e secure.zip file.txt

🔹 Basic Zip Command

Create compressed archives from files and directories using the fundamental zip command followed by your archive name and target files. The syntax zip archive.zip file1.txt file2.pdf generates a compressed bundle with automatic .zip extension addition. This basic compression reduces file sizes for efficient storage and transfer while maintaining original file structure. The command provides immediate compression feedback showing percentage reduction, making it ideal for quick archiving operations and routine file management tasks.

# Compress a single file
zip myarchive.zip report.pdf

# Compress multiple files
zip documents.zip file1.txt file2.txt file3.txt

# Compress with verbose output
zip -v archive.zip data.csv

Output:

adding: report.pdf (deflated 12%)

🔹 Compress Folders Recursively

Archive complete directory structures including all subdirectories and their contents using the recursive -r option with zip commands. This comprehensive approach preserves entire folder hierarchies, making it essential for project backups, deployment packages, and directory migrations. Executing zip -r project.zip myproject/ captures everything within the myproject folder, maintaining relative paths and directory relationships. The recursive flag ensures no files are overlooked, providing complete archival solutions for complex directory structures.

# Compress a folder and all its contents
zip -r project.zip myproject/

# Compress multiple folders
zip -r backup.zip folder1/ folder2/ folder3/

# Compress with maximum compression
zip -r -9 archive.zip documents/

Output:

adding: myproject/ (stored 0%)
adding: myproject/file1.txt (deflated 45%)
adding: myproject/subfolder/ (stored 0%)

🔹 Password Protected Archives

Securely handle encrypted ZIP files using the -P flag followed by the password or interactive prompts for enhanced security. While unzip -P password secure.zip works for quick operations, omitting the password from the command line triggers secure interactive input, protecting sensitive credentials from being stored in shell history. This dual approach balances convenience with security best practices, ensuring confidential data remains protected during extraction processes while maintaining compatibility with automation workflows.

# Create password-protected zip
zip -e secure.zip confidential.txt

# Encrypt entire folder
zip -er private.zip sensitive_data/

# Set password via command (less secure)
zip -P mypassword protected.zip file.txt

Output:

Enter password:
Verify password:
adding: confidential.txt (deflated 38%)

🔹 Exclude Files from Archive

Selectively omit specific files or patterns during compression using the exclusion -x flag followed by target patterns to skip. This fine-grained control allows you to create clean archives without temporary files, logs, or system artifacts. The command zip archive.zip folder/ -x "*.tmp" "*.log" compresses the directory while excluding temporary and log files. This exclusion capability is particularly valuable for creating production-ready deployment packages and minimizing archive sizes by removing unnecessary content.

# Exclude specific file
zip -r archive.zip folder/ -x "*.log"

# Exclude multiple patterns
zip -r backup.zip project/ -x "*.tmp" "*.cache" "node_modules/*"

# Exclude hidden files
zip -r clean.zip mydir/ -x "*/.*"

Output:

adding: folder/file1.txt (deflated 45%)
adding: folder/file2.txt (deflated 38%)

🔹 Update Existing Archive

Modify existing archives by adding new files or updating changed content using the -u and -f flags for efficient archive maintenance. The update option zip -u archive.zip newfile.txt adds new files or refreshes modified ones without recreating the entire archive. This incremental approach saves processing time and bandwidth when working with large archives, making it ideal for regular backup updates and version control operations where only portions of content change between archival cycles.

# Add new file to existing archive
zip archive.zip newfile.txt

# Update modified files only
zip -u archive.zip updated.txt

# Freshen existing entries
zip -f archive.zip folder/

Output:

updating: newfile.txt (deflated 42%)

🔹 Compression Levels

Balance compression ratio against processing speed using numeric levels from 0 (store) to 9 (maximum compression) with the -# flag. Level 0 provides fastest operation by simply storing files without compression, while level 9 delivers smallest file sizes through intensive processing. The default level 6 offers optimal balance for most applications. Specifying zip -9 archive.zip largefile.iso applies maximum compression for archival storage, while zip -0 archive.zip already_compressed.jpg quickly bundles pre-compressed files.

# No compression (store only)
zip -0 fast.zip largefile.bin

# Default compression
zip archive.zip file.txt

# Maximum compression
zip -9 small.zip documents/

Output:

adding: largefile.bin (stored 0%)
adding: file.txt (deflated 45%)
adding: documents/ (deflated 67%)

🧠 Test Your Knowledge

Which option is used to compress a folder recursively?