Chapter 42: Bash Compress (zip)
Bash Compress (zip), specifically the zip command in Bash/Linux! 😄
This is the tool most people think of first when they hear “compress files” — because .zip is the format everyone knows from Windows, macOS, phones, email attachments, and sharing on WhatsApp/Drive. In Linux/Bash, zip is very convenient when you need to send files to non-Linux users or when you want something simple that “just works” everywhere.
zip does two jobs in one command (unlike tar + gzip/xz/zstd):
- Bundles (archives) multiple files/folders into one file
- Compresses them at the same time
So no need for separate tar step — it’s all-in-one, which makes it beginner-friendly and cross-platform friendly.
Why Use zip in 2026?
- Super compatible — anyone can open .zip on Windows (built-in), macOS (built-in), Android/iOS, etc.
- Easy password protection
- Good for quick shares (documents, photos, small projects)
- But: usually worse compression ratio than .tar.xz or .tar.zst (bigger files), and doesn’t preserve Unix permissions/owners as perfectly as tar
If you’re only staying in Linux world → many prefer tar.gz / tar.xz / tar.zst (faster/better compression). But for “send to friend on Windows” or “upload to Google Drive” — zip wins.
Step 1: Make Sure zip is Installed
Most Ubuntu/Fedora have it, but check:
|
0 1 2 3 4 5 6 |
zip --version |
If not found:
|
0 1 2 3 4 5 6 7 8 |
sudo apt update && sudo apt install zip unzip # Ubuntu/Debian # or sudo dnf install zip unzip # Fedora |
unzip is the partner command to extract.
Basic Syntax
|
0 1 2 3 4 5 6 |
zip [options] archive_name.zip file1 file2 folder/ ... |
- First argument after options = name of the .zip file you want to create
- Then list files/folders to include
1. Compress Single or Multiple Files (Simplest)
|
0 1 2 3 4 5 6 |
zip mydocs.zip report.pdf notes.txt photo.jpg |
→ Creates mydocs.zip containing those 4 files.
Output looks like:
|
0 1 2 3 4 5 6 7 8 |
adding: report.pdf (deflated 12%) adding: notes.txt (stored 0%) adding: photo.jpg (deflated 5%) |
“deflated” = compressed, “stored” = no compression gain (already compressed like jpg).
2. Compress a Whole Folder (Recursive – Very Important!)
Use -r (recursive):
|
0 1 2 3 4 5 6 |
zip -r myproject.zip my_project_folder/ |
→ Includes everything inside my_project_folder/ (subfolders, files, hidden files if you include them).
Want current directory contents?
|
0 1 2 3 4 5 6 |
zip -r backup.zip . |
. = current folder
3. Include Hidden Files (dotfiles like .gitignore, .env)
By default zip skips files starting with . in some patterns. To force include:
|
0 1 2 3 4 5 6 |
zip -r full_backup.zip . -i .* |
Or just:
|
0 1 2 3 4 5 6 |
zip -r project.zip * .* |
4. Maximum Compression Level (-9)
Default is medium. For smallest size:
|
0 1 2 3 4 5 6 |
zip -r -9 super_small.zip big_folder/ |
→ -9 = slowest but best compression (takes longer, good for final archive)
-0 = no compression (fastest, just archive)
5. Add Password Protection (-e or –encrypt)
Very useful for sensitive files:
|
0 1 2 3 4 5 6 |
zip -r -e secret_project.zip my_folder/ |
→ Asks for password twice. Anyone opening needs the password.
(Note: basic zip encryption is not super secure in 2026 — for real security use 7z or gpg on top.)
6. Update Existing Zip (-u or –update)
Only add/change newer files:
|
0 1 2 3 4 5 6 |
zip -u myproject.zip new_file.txt changed_script.sh |
Great for incremental backups.
7. Delete Files from Zip (-d)
|
0 1 2 3 4 5 6 |
zip -d myproject.zip old_unused.txt |
8. Extract Zip Files (unzip Command)
|
0 1 2 3 4 5 6 |
unzip myproject.zip |
Extract to specific folder:
|
0 1 2 3 4 5 6 |
unzip myproject.zip -d extracted_folder/ |
List contents without extracting:
|
0 1 2 3 4 5 6 |
unzip -l myproject.zip |
9. Quick Practice Session (Do This Now!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# Create playground mkdir zip_test cd zip_test echo "Hello Hyderabad" > file1.txt echo "Bash rocks" > file2.txt mkdir photos touch photos/pic1.jpg photos/pic2.jpg # Zip files zip simple.zip file1.txt file2.txt # Zip folder recursive zip -r photos_backup.zip photos/ # Full project with max compression & password zip -r -9 -e my_secure_project.zip . # Check size savings ls -lh *.zip # Extract test mkdir extract_test unzip simple.zip -d extract_test/ cat extract_test/file1.txt |
See how easy and cross-platform it is?
Summary Table – zip Commands You’ll Use Every Day
| What you want | Command Example | Notes |
|---|---|---|
| Zip few files | zip docs.zip file1.pdf file2.txt | Simple |
| Zip folder recursive | zip -r project.zip my_project/ | Most common |
| Max compression | zip -r -9 archive.zip big_folder/ | Smallest size |
| Add password | zip -r -e secure.zip folder/ | Basic encryption |
| Update zip | zip -u archive.zip newfile.txt | Incremental |
| List contents | unzip -l archive.zip | Preview |
| Extract | unzip archive.zip or unzip -d dest/ archive.zip | Partner tool |
| Include hidden files | zip -r backup.zip . -i .* | Dotfiles |
Teacher’s Final Tips
- Use zip when sharing with Windows/macOS friends or uploading to cloud/email.
- Use tar -czf / tar -caf (zstd) when staying in Linux — better compression, preserves Unix permissions better.
- For huge folders → consider rsync first to sync, then zip only final version.
- Check size before/after: du -sh folder/ vs ls -lh archive.zip
Got it, boss? zip is your cross-platform compression friend — simple, everywhere supported, perfect for quick shares.
Any confusion? Want more on password strength, split zip files (big archives), or “zip vs tar.gz deep comparison”? Or next topic like “unzip advanced” or back to rsync?
Just say — teacher is ready in Hyderabad! Keep zipping smartly! 🐧📁🔐 😄
