How to Unzip Files in Linux

By 

•

Updated on

•

8 min read

Unzip Files in Linux

You download a WordPress release, a website theme, or a batch of files a colleague sent from Windows, and it arrives as a .zip file. Before you can use the files, you need to extract them.

ZIP is the most widely used archive file format that supports lossless data compression. A ZIP file is a data container containing one or more compressed files or directories, which can later be extracted to restore the original files. On Linux, you extract them from the terminal with the unzip command.

ZIP archives can also be password protected, which is why unzip may ask for a password before extracting encrypted files.

This article explains how to use the unzip command to extract and manage ZIP archives in Linux.

Quick Reference

OptionDescription
unzip file.zipExtract to current directory
unzip -d /path file.zipExtract to a specific directory
unzip file.zip file1 file2Extract only the listed files
unzip -p file.zip file.txtPrint a file to standard output
unzip -q file.zipQuiet mode, suppress output
unzip -o file.zipOverwrite existing files without prompting
unzip -n file.zipNever overwrite existing files
unzip -j file.zipFlatten directory structure on extract
unzip -l file.zipList archive contents
unzip -t file.zipTest archive integrity

For a printable quick reference, see the ZIP/UNZIP cheatsheet .

Installing unzip

Ubuntu Desktop and Debian with GNOME ship with unzip already installed. Server and minimal installations usually do not include it. If the unzip command is missing on your system, install it using your distribution’s package manager.

Install unzip on Ubuntu, Debian, and Derivatives

Terminal
sudo apt install unzip

Install unzip on Fedora, RHEL, and Derivatives

Terminal
sudo dnf install unzip

How to Unzip a ZIP File

In its simplest form, when used without any options, the unzip command extracts all files from the specified ZIP archive to the current directory.

For instance, suppose you downloaded the WordPress installation ZIP file. To unzip this file to the current directory, you would run the following command:

Terminal
unzip latest.zip

ZIP files do not store ownership information. The extracted files are owned by the user that runs the command.

You must have write permissions on the directory where you are extracting the ZIP archive.

Suppress the Output of the unzip Command

By default, unzip prints the names of all the files it is extracting, and it will also give you a summary once the extraction process is complete.

To suppress the printing of messages, use the -q option:

Terminal
unzip -q filename.zip

Unzip a ZIP File to a Different Directory

If you want to extract the contents of a ZIP file to a directory other than the present working directory, you can use the -d option. This allows you to specify the path of the destination directory where you want to extract the files.

Terminal
unzip filename.zip -d /path/to/directory

For example, to unzip the WordPress archive latest.zip to the /var/www/ directory, you would use the following command:

Terminal
sudo unzip latest.zip -d /var/www

In the command above, we are using sudo because the user we are logged in as typically does not have write permissions to the /var/www directory. When ZIP files are decompressed using sudo, the extracted files and directories are owned by the user root.

If the destination directory does not exist, unzip creates it. However, it creates only the last directory in the path. When a parent directory is missing too, the command fails:

Terminal
unzip latest.zip -d newdir/nested
output
Archive:  latest.zip
checkdir:  cannot create extraction directory: newdir/nested
           No such file or directory

Create the full path first with mkdir -p , then run unzip again.

Unzip a Password Protected ZIP File

If you have a ZIP file that is protected with a password and you want to extract its contents, you can use the unzip command with the -P option. This option allows you to specify the password required to open the file. Once you enter the correct password, the contents of the ZIP file will be extracted to the current directory.

Terminal
unzip -P PasswOrd filename.zip

Typing a password on the command line is insecure and should be avoided. A more secure option is to run the command without using the -P option.

If the ZIP file is password protected and you do not use -P, unzip will prompt you to enter the password:

Terminal
unzip filename.zip

Type the password and press Enter.

output
Archive:  filename.zip
[filename.zip] file.txt password:

unzip will use the same password for all encrypted files in the archive as long as it is correct.

Test a ZIP File Without Extracting

To test a ZIP archive for errors without extracting it, use the -t option:

Terminal
unzip -t filename.zip

Exclude Files when Unzipping a ZIP File

When you want to exclude specific files or directories from being extracted from an archive, use the -x option, followed by a list of the archive files you want to exclude from the extraction process, separated by spaces.

Terminal
unzip filename.zip -x file1-to-exclude file2-to-exclude

In the following example, we are extracting all files and directories from the ZIP archive except the .git directory:

Terminal
unzip filename.zip -x "*.git/*"

Overwrite Existing Files

Suppose you have already extracted a ZIP file and run the same command again.

Terminal
unzip latest.zip

By default, unzip asks whether you want to overwrite only the current file, overwrite all files, skip extraction of the current file, skip extraction of all files, or rename the current file.

output
Archive:  latest.zip
replace wordpress/xmlrpc.php? [y]es, [n]o, [A]ll, [N]one, [r]ename:

If you want to overwrite existing files without being prompted, use the -o option:

Terminal
unzip -o filename.zip

Use this option with caution. If you previously made any changes to the files, the changes are lost. In automation, -o is convenient, but it can overwrite files silently.

Unzip a ZIP File Without Overwriting Existing Files

Suppose you have unzipped a ZIP file and modified some of its files. However, you mistakenly deleted a few files that you now need to recover from the ZIP archive. What you want to do now is to restore the deleted files from the ZIP archive while keeping the changes you have made.

In this scenario, use the -n option, which instructs unzip to skip the extraction of a file if it already exists in the target directory:

Terminal
unzip -n filename.zip

Unzip Multiple ZIP Files

You can use wildcard patterns to match multiple archives.

For instance, if you have multiple ZIP files in your current working directory , you can unzip all files using only one command:

Terminal
unzip '*.zip'

Note the single quotes around *.zip. If you forget to quote the argument, the shell expands the wildcard before unzip runs. unzip then treats the first file as the archive and the other files as names to extract from it:

output
Archive:  archive1.zip
caution: filename not matched:  archive2.zip

Nothing is extracted in this case. Add the quotes and run the command again.

List the Contents of a ZIP File

To list the contents of a ZIP file, use the -l option:

Terminal
unzip -l filename.zip

In the example below, we are listing all WordPress installation files:

Terminal
unzip -l latest.zip

The output will look like this:

output
Archive:  latest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2023-08-02 22:39   wordpress/
     3065  2021-08-31 18:31   wordpress/xmlrpc.php
      364  2021-12-19 12:20   wordpress/wp-blog-header.php
     7415  2023-03-18 17:13   wordpress/readme.html
...
...
    21323  2023-03-09 01:15   wordpress/wp-admin/themes.php
     8353  2022-09-10 18:20   wordpress/wp-admin/options-reading.php
     4620  2021-10-24 00:12   wordpress/wp-trackback.php
     1889  2023-05-03 00:11   wordpress/wp-comments-post.php
---------                     -------
 27271400                     1648 files

To show a more detailed listing, use -v:

Terminal
unzip -v filename.zip

Extract Specific Files from a ZIP Archive

You do not always need the whole archive. To extract only certain files, pass their names after the archive name:

Terminal
unzip latest.zip wordpress/wp-config-sample.php
output
Archive:  latest.zip
 extracting: wordpress/wp-config-sample.php

The name must match the path stored in the archive, including any leading directories. If you type only wp-config-sample.php, unzip prints caution: filename not matched and extracts nothing. When you are not sure how a file is stored, list the archive first with unzip -l.

To extract a whole directory from the archive, use a wildcard pattern. Quote it so the shell does not expand it:

Terminal
unzip latest.zip "wordpress/wp-content/*"

The extracted files keep their directory structure. The command above creates wordpress/wp-content/ in the current directory.

Flatten Directory Structure When Extracting

By default, unzip recreates the directory structure stored inside the archive. If you want all files extracted into a single flat directory without any subdirectories, use the -j (junk paths) option:

Terminal
unzip -j filename.zip

This is useful when you only need the files themselves and do not care about the original folder layout inside the archive.

Troubleshooting

A few errors come up often when extracting ZIP archives. Here is how to recognize and fix them.

unzip: command not found
The unzip package is not installed. Install it with your package manager as shown in the Installing unzip section above.

End-of-central-directory signature not found
This usually means the archive is incomplete or corrupt, most often from an interrupted download. Download the file again, then verify it before extracting with unzip -t filename.zip.

skipping: filename unsupported compression method 99
Method 99 is AES encryption, which the standard unzip utility cannot decrypt. Depending on the tool that created the archive, the message can also read need PK compat. v5.1 (can do v4.6). Extract the archive with 7z instead: 7z x filename.zip.

warning [archive.zip]: zipfile claims to be last disk of a multi-part archive
The archive was split into parts such as archive.z01 and archive.z02, and unzip cannot extract split archives directly. Place all parts in the same directory, join them with zip -s 0 archive.zip --out joined.zip, and then run unzip joined.zip.

Conclusion

unzip is a command-line tool that allows you to list, test, and extract compressed ZIP archives.

To create a ZIP archive on a Linux system, use the zip command . For .tar.gz and other compressed tarballs, see how to extract a tar.gz file .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page