zip
Overview
The zip
command creates compressed archive files in ZIP format. It’s widely compatible across different operating systems and supports various compression methods.
Syntax
zip [options] archive.zip file1 file2...
unzip [options] archive.zip
Common Options
Option | Description |
---|---|
-r |
Recursive (include subdirectories) |
-u |
Update existing archive |
-f |
Freshen existing entries |
-d |
Delete entries from archive |
-m |
Move files to archive |
-j |
Junk directory paths |
-e |
Encrypt archive |
-x pattern |
Exclude files |
-i pattern |
Include only files |
-v |
Verbose output |
-q |
Quiet mode |
Compression Levels
Level | Description |
---|---|
-0 |
No compression (store only) |
-1 |
Fastest compression |
-6 |
Default compression |
-9 |
Best compression |
Key Use Cases
- Create portable archives
- Compress multiple files
- Cross-platform file sharing
- Backup directories
- Distribute software packages
Examples with Explanations
Example 1: Create Basic Archive
zip archive.zip file1.txt file2.txt
Creates archive containing specified files
Example 2: Recursive Directory Archive
zip -r backup.zip /home/user/documents/
Archives entire directory structure
Example 3: Extract Archive
unzip archive.zip
Extracts all files from archive
Archive Management
Add files to existing archive:
zip -u archive.zip newfile.txt
Delete files from archive:
zip -d archive.zip oldfile.txt
List archive contents:
unzip -l archive.zip
Advanced Operations
Password protection:
zip -e secure.zip sensitive.txt
Exclude patterns:
zip -r archive.zip directory/ -x "*.tmp"
Update only newer files:
zip -u archive.zip *.txt
Unzip Options
Option | Description |
---|---|
-l |
List contents |
-t |
Test archive |
-d dir |
Extract to directory |
-j |
Junk paths |
-o |
Overwrite without prompting |
-n |
Never overwrite |
-q |
Quiet mode |
-v |
Verbose listing |
Common Usage Patterns
Backup with date:
zip backup-$(date +%Y%m%d).zip *.txt
Exclude hidden files:
zip -r archive.zip directory/ -x "*/.*"
Extract to specific directory:
unzip archive.zip -d /target/directory/
Performance Analysis
- Good compression ratios
- Moderate CPU usage
- Memory efficient
- Fast extraction
- Good for mixed file types
File Compatibility
- Cross-platform support
- Windows native support
- macOS built-in support
- Linux standard tool
- Mobile device support
Additional Resources
Best Practices
- Use descriptive archive names
- Test archives after creation
- Consider compression vs speed trade-offs
- Use encryption for sensitive data
- Verify extraction success
Security Considerations
- Password protect sensitive archives
- Verify archive integrity
- Be cautious with zip bombs
- Check extraction paths
- Validate archive sources
Integration Examples
With find:
find . -name "*.log" | zip logs.zip -@
Automated backup:
zip -r backup-$(date +%Y%m%d).zip /important/data/
Selective archiving:
zip -r project.zip . -x "node_modules/*" "*.git/*"
Troubleshooting
- Archive corruption issues
- Path length limitations
- Permission problems
- Disk space errors
- Character encoding issues
Archive Testing
Test integrity:
unzip -t archive.zip
Verbose test:
zip -T archive.zip
Check specific files:
unzip -t archive.zip file.txt