unzip
Overview
The unzip
command extracts files from ZIP archives. It’s the counterpart to zip
and provides various options for extraction, testing, and listing archive contents.
Syntax
unzip [options] archive.zip [file(s)] [-x excluded_files] [-d extract_dir]
Common Options
Option | Description |
---|---|
-l |
List archive contents |
-t |
Test archive integrity |
-d dir |
Extract to directory |
-j |
Junk paths (flatten directory structure) |
-o |
Overwrite files without prompting |
-n |
Never overwrite existing files |
-u |
Update files (extract if newer) |
-f |
Freshen existing files only |
-v |
Verbose listing |
-q |
Quiet mode |
-x files |
Exclude specified files |
-p |
Extract to pipe (stdout) |
Key Use Cases
- Extract ZIP archives
- List archive contents
- Test archive integrity
- Selective file extraction
- Archive maintenance
Examples with Explanations
Example 1: Basic Extraction
unzip archive.zip
Extracts all files to current directory
Example 2: Extract to Directory
unzip archive.zip -d /target/directory/
Extracts files to specified directory
Example 3: List Contents
unzip -l archive.zip
Lists files in archive without extracting
Example 4: Test Archive
unzip -t archive.zip
Tests archive integrity without extracting
Selective Extraction
Extract specific files:
unzip archive.zip file1.txt file2.txt
Extract by pattern:
unzip archive.zip "*.txt"
Exclude files:
unzip archive.zip -x "*.tmp" "temp/*"
Advanced Options
Option | Description |
---|---|
-C |
Match filenames case-insensitively |
-L |
Convert filenames to lowercase |
-a |
Auto-convert text files |
-b |
Treat all files as binary |
-M |
Pipe through more |
-z |
Display archive comment |
-Z |
Display zipinfo-style listing |
Common Usage Patterns
Safe extraction:
unzip -n archive.zip
Overwrite all:
unzip -o archive.zip
Flatten directory structure:
unzip -j archive.zip
Archive Information
Detailed listing:
unzip -v archive.zip
Archive comment:
unzip -z archive.zip
Technical info:
unzip -Z archive.zip
Performance Analysis
- Fast extraction for most archives
- Memory usage depends on compression method
- Good for moderate-sized archives
- Handles password-protected archives
- Efficient selective extraction
Best Practices
- Test archives before extraction
- Use appropriate extraction directory
- Check available disk space
- Verify file permissions after extraction
- Handle filename conflicts appropriately
Security Considerations
Zip bomb protection:
unzip -l archive.zip | awk '{sum+=$1} END {if(sum>1000000000) print "Large archive warning"}'
Path traversal protection:
unzip -j archive.zip # Flatten paths
Verify archive source
Check extracted file permissions
Scan for malicious content
Password-Protected Archives
Extract with password:
unzip -P password archive.zip
Prompt for password:
unzip archive.zip # Will prompt if password needed
Integration Examples
Automated extraction:
for archive in *.zip; do unzip -q "$archive" -d "${archive%.zip}" done
Backup restoration:
unzip -o backup.zip -d /restore/location/
Selective processing:
unzip -p archive.zip "*.txt" | grep "pattern"
Error Handling
Check extraction success:
if unzip -t archive.zip > /dev/null 2>&1; then unzip archive.zip else echo "Archive is corrupted" fi
Handle missing files:
unzip archive.zip 2>/dev/null || echo "Extraction failed"
Scripting Applications
Batch extraction:
#!/bin/bash for zip_file in *.zip; do echo "Extracting $zip_file" unzip -q "$zip_file" -d "${zip_file%.zip}" done
Archive validation:
validate_archive() { local archive="$1" if unzip -t "$archive" >/dev/null 2>&1; then echo "Valid: $archive" return 0 else echo "Invalid: $archive" return 1 fi }
Troubleshooting
- Corrupted archives
- Insufficient disk space
- Permission issues
- Filename encoding problems
- Path length limitations
Output Formats
Simple list:
unzip -l archive.zip | tail -n +4 | head -n -2
Size information:
unzip -l archive.zip | grep -E "^\s*[0-9]"
Date sorting:
unzip -v archive.zip | sort -k7,8