The unzip Command Explained

The unzip Command Explained

unzip is the counterpart to zip, handling extraction of the cross-platform archive format most commonly encountered when receiving files from Windows or macOS users, or downloading anything packaged as a .zip.

Basic Usage

unzip archive.zip

Extracts all contents into the current directory, recreating any folder structure stored in the archive.

Extracting to a Specific Directory

unzip archive.zip -d /target/path

The -d flag specifies a destination directory. Unlike tar, unzip will create the target directory if it doesn’t already exist, rather than requiring it to be created first.

Listing Contents Without Extracting

unzip -l archive.zip
Archive:  archive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     4096  2026-07-01 10:22   data/config.yml
   891200  2026-07-01 10:22   data/export.csv
        0  2026-07-01 10:20   data/logs/
---------                     -------
   895296                     3 files

Checking contents before extracting is good practice, especially for archives from an unfamiliar or untrusted source, since it shows exactly what will be written to disk without committing to it.

Handling Existing Files

By default, if extraction would overwrite files that already exist, unzip prompts interactively for each conflict (overwrite, skip, rename, or apply to all remaining). In scripts or automated contexts where a prompt would hang the process:

unzip -o archive.zip     # overwrite everything without asking
unzip -n archive.zip     # skip existing files without asking, never overwrite

Extracting Specific Files Only

unzip archive.zip data/config.yml
# with a wildcard, quoted to prevent shell expansion
unzip archive.zip "*.txt"

Paths must match what’s shown in unzip -l output. Quoting wildcard patterns prevents the shell from trying to expand them against local files before passing them to unzip.

Password-Protected Archives

unzip archive.zip
# prompts interactively for the password

or non-interactively:

unzip -P yourpassword archive.zip

The interactive prompt is the safer option since -P exposes the password in shell history and to any other user able to view running processes on the system. Use -P only in scripts where the password is being supplied from a secure source (like a secrets manager) rather than typed directly into a command.

Dealing With Corrupted Archives

A CRC (cyclic redundancy check) error during extraction means the data doesn’t match the checksum recorded when the archive was created, indicating corruption somewhere along the way: an incomplete download, a storage issue, or a problem when the archive was originally built.

unzip -t archive.zip

-t tests the archive’s integrity without extracting anything, checking every file’s checksum and reporting which ones fail.

unzip -F archive.zip

-F attempts to fix a corrupted archive by looking for the central directory structure and recovering what it can. For more aggressive repair, the zip tool itself offers a repair mode:

zip -FF archive.zip --out repaired.zip

Neither repair method guarantees full recovery; success depends on how much of the underlying archive structure survived whatever caused the corruption in the first place.

Quiet Mode for Scripts

unzip -q archive.zip -d /target

-q suppresses the per-file listing normally printed during extraction, useful in scripts where only the final result (success or failure) matters, not a scrolling list of every extracted file.

Frequently Asked Questions

How do I extract a zip file to a specific directory instead of the current one?

Use the -d flag followed by the target directory, for example unzip archive.zip -d /target/path. Unlike tar, unzip will create the target directory automatically if it does not already exist, rather than requiring it to be created beforehand.

How can I see what is inside a zip file without extracting it?

Run unzip -l archive.zip to list the contents, including file names, sizes, and modification dates, without extracting anything to disk. This is worth doing before extracting an archive from an untrusted source, since it shows exactly what files and paths would be written.

What happens if I try to extract a zip file and files with the same names already exist?

By default, unzip prompts interactively for each conflicting file, asking whether to overwrite, skip, rename, or overwrite all remaining conflicts. In scripts or non-interactive contexts, use -o to overwrite all conflicts without prompting, or -n to skip all existing files without prompting, whichever behavior matches what the script needs.

How do I extract a password-protected zip file?

Use unzip -P yourpassword archive.zip to supply the password directly on the command line, though this exposes the password in shell history and process listings. Running unzip archive.zip without -P and letting it prompt interactively for the password is safer since the password is not stored in shell history or visible to other users checking running processes.

Can I extract only specific files from a zip archive instead of everything?

Yes, list the specific file paths after the archive name, for example unzip archive.zip path/to/specific-file.txt. The paths must match exactly as shown in unzip -l output. Wildcards also work when quoted to prevent the shell from expanding them first, such as unzip archive.zip “*.txt”.

What does it mean if unzip reports a zip file is corrupted or gives a CRC error?

A CRC (cyclic redundancy check) error means the data read for a file does not match the checksum stored in the archive, indicating the file was corrupted during transfer, storage, or the original creation of the archive. Try unzip -F archive.zip (or zip -FF for more aggressive repair using the zip tool itself) to attempt to fix a corrupted archive, though success depends on how much of the archive structure is still intact and there is no guarantee of full recovery.