The unzip command in Linux extracts files from .zip archives via the terminal. Use it to list, test, or extract archives, including password protected zips. Typical usage is “unzip file.zip” or “unzip file.zip -d /path”. It’s lightweight, script friendly, and available on most distributions via the “unzip” package.
If you’re working with compressed files on a server or local machine, the unzip command in Linux is a fast, reliable way to extract .zip archives. This guide covers installation, syntax, common options, real world examples, security considerations, and troubleshooting, everything you need to confidently use unzip in production or development environments.
What is the unzip Command in Linux?
unzip is a command line utility that extracts and inspects .zip archives. It supports listing contents, verifying integrity, selective extraction, password protected archives, and robust wildcard matching. It’s ideal for automation (shell scripts, CI/CD), remote servers via SSH, and quick terminal workflows where GUI tools aren’t available.
Install unzip on Popular Linux Distributions
Most servers don’t include unzip by default. Install it with your package manager:
Debian/Ubuntu:
sudo apt update && sudo apt install -y unzip zipRHEL/CentOS/AlmaLinux/Rocky:
sudo dnf install -y unzip zip
# older releases: sudo yum install -y unzip zipFedora:
sudo dnf install -y unzip zipArch/Manjaro:
sudo pacman -S unzip zipAlpine:
sudo apk add unzip zipBasic Syntax and Common Options
The basic syntax is simple:
unzip [options] archive.zip [file_pattern ...] -d target_directoryQuick Start: Extract a Zip
# Extract to current directory
unzip site_backup.zip
# Extract to a specific directory
unzip site_backup.zip -d /var/www/htmlList Files Without Extracting (-l)
# Short listing
unzip -l site_backup.zip
# Verbose listing with attributes (-v shows more details)
unzip -v site_backup.zipTest Archive Integrity (-t)
unzip -t site_backup.zipThis validates that the archive isn’t corrupted, useful for backups and CI/CD pipelines.
Control Overwrites (-n and -o)
By default, unzip prompts before overwriting existing files. Use:
# Never overwrite existing files
unzip -n site_backup.zip
# Overwrite existing files without prompting
unzip -o site_backup.zipSelective Extraction with Patterns and Exclusions
# Extract only PHP files from public/ directory
unzip site_backup.zip "public/*.php" -d /var/www/html
# Exclude specific paths or file types (-x)
unzip site_backup.zip -x "*/node_modules/*" "*.log" -d /var/www/htmlQuotes prevent your shell from interpreting wildcards, letting unzip handle patterns internally.
Password Protected Archives (-P)
For encrypted zips, you can provide a password. Prefer interactive prompts in shared environments.
# Inline password (avoid in shared shells or history-enabled sessions)
unzip -P "StrongPassword" secrets.zip -d ~/secure
# Safer: omit -P and let unzip prompt for the password
unzip secrets.zip -d ~/securePreserve or Ignore Folder Structure (-j)
Use “junk paths” to flatten directories during extraction:
# Extract everything into one folder without subdirectories
unzip -j images.zip -d ./images_flatUpdate vs. Freshen (-u and -f)
Useful for incremental deployments or syncing.
# Update: extract only files that are newer than existing ones or missing
unzip -u build.zip -d /var/www/app
# Freshen: update only files that already exist (don’t create new files)
unzip -f build.zip -d /var/www/appQuiet and Non Interactive Modes (-q, -o, -n)
For scripts and cron jobs, reduce noise and avoid prompts:
# Quiet + overwrite
unzip -q -o release.zip -d /opt/app
# Quiet + never overwrite
unzip -q -n assets.zip -d /opt/app/assetsPractical Examples and Real World Use Cases
Batch Unzip Multiple Archives
If a directory contains many .zip files, automate extraction:
mkdir -p ~/archives/extracted
for z in ~/archives/*.zip; do
[ -e "$z" ] || continue
unzip -n "$z" -d ~/archives/extracted
doneExtract Only Logs or Specific File Types
# Only .log files
unzip logs_backup.zip "*.log" -d ./logs
# Only images from nested folders
unzip media.zip "*.png" "*.jpg" "*.webp" -d ./public/imagesRecursive Unzip in Subdirectories
Unzip all archives found recursively under a path:
find /data/backups -type f -name "*.zip" -print0 | while IFS= read -r -d '' z; do
unzip -n "$z" -d "${z%.zip}"
doneHandle Filenames with Spaces or Special Characters
# Quote file specs; use -O to set character encoding if needed
unzip -O UTF-8 "backup with spaces.zip" -d ./restoreUse the -O option when archives were created on systems with specific encodings.
List, Grep, and Filter Contents Before Extracting
# Find a file in the archive without extracting
unzip -l app.zip | grep -i "config.php"Permissions, Ownership, and Security Considerations
When extracting on servers, think about file permissions and security.
- Ownership: unzip extracts as the current user. If deploying to web roots (e.g., /var/www), use a privileged account carefully or extract to a temp folder and move with correct ownership (chown -R www-data:www-data /var/www/html).
- Permissions: unzip attempts to restore stored modes. You may need chmod after extraction to align with your security policy (e.g., files 0644, directories 0755).
- Symlinks: Archives can contain symlinks. Extract only from trusted sources to avoid path traversal or unsafe link targets.
- Environment: Avoid inlining passwords (-P) on multi-user systems; prefer interactive prompts or environment injection in CI with care.
- Encoding: Use -O to specify filename encoding; mismatched encodings can create garbled filenames.
Troubleshooting Common unzip Errors
- End-of-central-directory signature not found: The file is not a valid zip or is truncated. Re-download, verify checksum, or ensure you’re not pointing at a HTML error page saved as .zip.
- Cannot find or open file.zip: Confirm the path and case sensitivity. Use absolute paths or cd to the correct directory.
- Skipping: file already exists; use -o to overwrite”: Add -o or -n per your desired behavior, or remove the destination first.
- Encrypted file: use -P to provide a password”: Supply a password or re-create the archive without encryption.
- Multi-part archives (.z01, .z02… .zip): Ensure all parts are present in the same directory and run unzip on the .zip file.
- Corruption/CRC errors: Try unzip -t first; if failing, request a fresh archive or attempt recovery with zip -FF (on the creator’s side).
Zip vs. tar.gz: Which Should You Use?
- Use .zip when you need cross platform compatibility (Windows/macOS/Linux), per file compression, and easy GUI handling.
- Use .tar.gz for Linux native workflows, better compression ratios on large codebases, and preserving UNIX permissions and ownership in a single archive.
- For server deployments, tar.gz is common, but .zip is perfectly fine, especially when collaborating with Windows users.
Automating unzip in CI/CD and Cron Jobs
Make extractions deterministic and safe in automation. Use non interactive flags, ensure idempotency, and log results.
# Example: deploy build artifact quietly and overwrite
set -euo pipefail
ARTIFACT="/tmp/build.zip"
DEST="/var/www/app"
if unzip -t "$ARTIFACT" >/dev/null; then
mkdir -p "$DEST"
unzip -q -o "$ARTIFACT" -d "$DEST"
find "$DEST" -type d -exec chmod 755 {} \;
find "$DEST" -type f -exec chmod 644 {} \;
chown -R www-data:www-data "$DEST"
echo "Deployment complete."
else
echo "Invalid artifact. Aborting." >&2
exit 1
fiFor zero downtime strategies, extract into a release directory and switch a symlink atomically after validation.
Advanced Tips and Less Known Options
- Preserve timestamps: unzip preserves stored timestamps; verify with ls -l.
- Case sensitivity: Patterns are case sensitive unless your system locale changes behavior; use character classes (e.g., “*.{JPG,jpg}”).
- Funzip for streaming: funzip reads from stdin and writes decompressed data to stdout, handy in pipelines.
- zipinfo for metadata: zipinfo -1 archive.zip prints a simple list of filenames.
- Exclude multiple directories: combine -x with quoted globs to skip node_modules, vendor, or caches.
FAQs
How Do I Unzip a File in Linux to a Specific Directory?
Use the -d option. Example: unzip archive.zip -d /path/to/target. The target directory will be created if it doesn’t exist (when possible).
How Can I Unzip Without Overwriting Existing Files?
Use -n to “never overwrite.” For example: unzip -n archive.zip. To always overwrite without prompts, use -o.
How Do I Unzip a Password Protected Zip in Linux?
Run unzip archive.zip and enter the password when prompted. Or supply it with -P “password” (not recommended on shared systems due to shell history exposure).
How Do I List the Contents of a Zip File Without Extracting?
Run unzip -l archive.zip for a concise list, or unzip -v archive.zip for verbose details like compression method and file sizes.
Can I Extract Only Certain Files or Folders From a Zip?
Yes. Specify patterns and exclusions. Examples: unzip app.zip “public/*.php” -d ./site or unzip app.zip -x “*/tests/*” “*.md”. Always quote patterns to let unzip process them.
What Does End of Central Directory Signature not Found Mean?
The file is likely not a valid zip, is incomplete, or you downloaded an error page. Verify the URL, re-download, or check integrity with unzip -t and checksums.
Is Unzip Installed By Default on Linux?
Not always. Install it via your package manager, e.g., sudo apt install unzip on Ubuntu/Debian or sudo dnf install unzip on Fedora/RHEL-based systems.
Conclusion
Mastering the unzip command in Linux gives you fast, scriptable control over .zip archives, perfect for developers, sysadmins, and site owners. Whether you’re deploying to production, restoring backups, or automating CI/CD, the options above will help you extract safely and efficiently. For optimized hosting with SSH and reliable file operations, explore YouStable’s plans.