Bash Extract (unzip)

Learn to extract and decompress zip archives

📂 What is Unzip?

Unzip is the extraction utility for zip archives that decompresses and restores files to their original state. It allows you to access compressed files, view archive contents, and extract specific files selectively.


# Simple unzip example
unzip archive.zip
                                    

Output:

Archive: archive.zip
inflating: file.txt

Key Unzip Concepts

📦

Extract All

Extract entire archive contents

unzip archive.zip
📋

List Contents

View files without extracting

unzip -l archive.zip
📍

Specific Location

Extract to custom directory

unzip archive.zip -d /path/
🔍

Selective Extract

Extract specific files only

unzip archive.zip file.txt

🔹 Basic Unzip Command

The standard unzip command extracts all contents from ZIP archives to the current directory, automatically recreating stored folder structures and preserving file attributes. Executing unzip archive.zip processes the entire archive, displaying each extracted file and created directory during the operation. This comprehensive extraction maintains the original organization exactly as archived, including subdirectory hierarchies and permission settings where supported. The verbose output provides confirmation of successful extraction and allows users to monitor progress, particularly valuable for large archives or those containing complex directory structures.

# Extract all files to current directory
unzip archive.zip

# Extract with verbose output
unzip -v archive.zip

# Quiet extraction (no output)
unzip -q documents.zip

Output:

Archive: archive.zip
inflating: file1.txt
inflating: file2.txt
creating: folder/

🔹 List Archive Contents

Previewing ZIP archive contents with unzip -l before extraction provides valuable information about stored files, their sizes, compression ratios, and modification dates. This non-destructive examination helps verify archive integrity, identify specific files for selective extraction, estimate storage requirements, and confirm archive purpose before committing to full extraction. The detailed listing includes original file sizes, compressed sizes, compression percentages, and timestamps, offering comprehensive insight into archive composition. This preview capability supports informed decision-making for archive management and extraction planning.

# List files in archive
unzip -l archive.zip

# Detailed listing with permissions
unzip -Z archive.zip

# List only file names
unzip -Z1 archive.zip

Output:

Archive: archive.zip
Length Date Time Name
-------- ---- ---- ----
1024 10-10 14:30 file1.txt
2048 10-10 14:31 file2.txt

🔹 Extract to Specific Directory

The unzip command allows you to extract archive contents to a designated directory using the -d flag followed by your preferred path. This is particularly useful for organizing extracted files into specific folders rather than cluttering your current working directory. For instance, unzip archive.zip -d /home/user/extracted/ creates the target directory if it doesn't exist and places all extracted files there. This method helps maintain a clean file system structure and ensures extracted content is immediately organized according to your project requirements.

# Extract to specific folder
unzip archive.zip -d /home/user/extracted/

# Extract to relative path
unzip backup.zip -d ./restore/

# Extract to parent directory
unzip files.zip -d ../output/

Output:

Archive: archive.zip
creating: /home/user/extracted/
inflating: /home/user/extracted/file.txt

🔹 Extract Specific Files

You can selectively extract individual files from archives by specifying their exact names or using wildcard patterns for multiple matches. This targeted extraction saves time and storage space when you only need certain files from large archives. For example, unzip archive.zip "*.txt" extracts all text files, while unzip archive.zip document.pdf retrieves only the PDF file. This approach is invaluable for developers working with large dependency archives or content creators managing asset libraries.

# Extract single file
unzip archive.zip file.txt

# Extract multiple specific files
unzip archive.zip file1.txt file2.txt

# Extract files matching pattern
unzip archive.zip "*.pdf"

# Extract from specific folder
unzip archive.zip "docs/*"

Output:

Archive: archive.zip
inflating: file.txt

🔹 Overwrite Options

Control file overwrite behavior during extraction with dedicated flags that manage conflicts between existing files and archive contents. The -o option overwrites all existing files without prompting, while -n preserves existing files and skips extraction of duplicates. For intelligent updates, use -u to only overwrite when archive files are newer. These options streamline automated scripts and prevent accidental data loss while maintaining efficient workflow management across development and deployment environments.

# Overwrite all files without prompting
unzip -o archive.zip

# Never overwrite existing files
unzip -n archive.zip

# Update only newer files
unzip -u archive.zip

# Prompt for each file (default)
unzip archive.zip

Output:

Archive: archive.zip
replace file.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:

🔹 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.

# Extract with password prompt
unzip secure.zip

# Provide password in command (less secure)
unzip -P mypassword secure.zip

# Extract encrypted archive quietly
unzip -q -P password protected.zip

Output:

Archive: secure.zip
[secure.zip] file.txt password:
inflating: file.txt

🔹 Test Archive Integrity

Verify archive structure and content validity before extraction using the -t testing option to detect corruption or damage. This preventative check reads through all compressed files, validating their integrity and ensuring successful future extractions. The command unzip -t archive.zip performs comprehensive testing and reports any errors, saving time and preventing partial extractions from corrupted archives. Regular integrity testing is crucial for backup verification and quality assurance in data management pipelines.

# Test archive integrity
unzip -t archive.zip

# Test with verbose output
unzip -tv archive.zip

# Test specific file
unzip -t archive.zip file.txt

Output:

Archive: archive.zip
testing: file1.txt OK
testing: file2.txt OK
No errors detected in archive.zip

🧠 Test Your Knowledge

Which option lists the contents of a zip file without extracting?